Ways to illicit polymorphic properties in regular C?










1














Hi I am currently attempting to learn C and I was wondering if there is a way to attain polymorphism in structures which contain a list of other different type of structures?



An example case of this is as such:



 #include <stdlib.h>
#include <stdio.h>

typedef void (*update_t)(void *);

typedef struct entity entity_t;
typedef struct compA compA_t;
typedef struct compB compB_t;


struct compA

update_t update;
;

struct compB

update_t update;
;

struct entity
update_t update;
int curSize;
void **components;
;


void compA_update(void *c)
printf("updating: componentAn");


compA_t *compA_create()
compA_t *c = malloc(sizeof(compA_t));
c->update = compA_update;
return c;


void compB_update(void *c)
printf("updating: componentBn");



compB_t *compB_create()
compB_t *c = malloc(sizeof(compB_t));
c->update = compB_update;
return c;



void entity_update(void *en)
entity_t *e = (entity_t *)en;
for(int i = 0; i < e->curSize; i++)
//would like to somehow update all the components with one line just iterating through the array but does not seem possible

return;




entity_t *entity_create()
entity_t *e = malloc(sizeof(entity_t));
e->curSize = 0;
e->update = entity_update;
calloc(32, sizeof(void *));
return e;


void add_component(entity_t *e, void *c)
printf("%dn", e->curSize);
e->components[e->curSize] = c;
e->curSize++;
return;





int main(void)

entity_t *e = entity_create();
compA_t *a = compA_create();
compB_t *b = compB_create();
add_component(e, a);
add_component(e, b);

e->update(e);

return 0;



So far my approach to this problem has been solved with void pointer arrays of a tuple structure which contains a enum type which identifies the structure as well as the structure itself and then in a potential update function a fairly ugly switch statement has to be implemented with a case for each specific type.



Is there a better way to do this? As the switch approach will get fairly crazy pretty fast if there are a lot of different types within the array. which means one must explicitly add cases for each type and every case does exactly the same thing, which in this case is call a function pointer named "update".










share|improve this question





















  • You can try data polymorphism instead of function pointer. That is, different data produce different behavior, using the same code.
    – JiaHao Xu
    Nov 10 '18 at 13:19
















1














Hi I am currently attempting to learn C and I was wondering if there is a way to attain polymorphism in structures which contain a list of other different type of structures?



An example case of this is as such:



 #include <stdlib.h>
#include <stdio.h>

typedef void (*update_t)(void *);

typedef struct entity entity_t;
typedef struct compA compA_t;
typedef struct compB compB_t;


struct compA

update_t update;
;

struct compB

update_t update;
;

struct entity
update_t update;
int curSize;
void **components;
;


void compA_update(void *c)
printf("updating: componentAn");


compA_t *compA_create()
compA_t *c = malloc(sizeof(compA_t));
c->update = compA_update;
return c;


void compB_update(void *c)
printf("updating: componentBn");



compB_t *compB_create()
compB_t *c = malloc(sizeof(compB_t));
c->update = compB_update;
return c;



void entity_update(void *en)
entity_t *e = (entity_t *)en;
for(int i = 0; i < e->curSize; i++)
//would like to somehow update all the components with one line just iterating through the array but does not seem possible

return;




entity_t *entity_create()
entity_t *e = malloc(sizeof(entity_t));
e->curSize = 0;
e->update = entity_update;
calloc(32, sizeof(void *));
return e;


void add_component(entity_t *e, void *c)
printf("%dn", e->curSize);
e->components[e->curSize] = c;
e->curSize++;
return;





int main(void)

entity_t *e = entity_create();
compA_t *a = compA_create();
compB_t *b = compB_create();
add_component(e, a);
add_component(e, b);

e->update(e);

return 0;



So far my approach to this problem has been solved with void pointer arrays of a tuple structure which contains a enum type which identifies the structure as well as the structure itself and then in a potential update function a fairly ugly switch statement has to be implemented with a case for each specific type.



Is there a better way to do this? As the switch approach will get fairly crazy pretty fast if there are a lot of different types within the array. which means one must explicitly add cases for each type and every case does exactly the same thing, which in this case is call a function pointer named "update".










share|improve this question





















  • You can try data polymorphism instead of function pointer. That is, different data produce different behavior, using the same code.
    – JiaHao Xu
    Nov 10 '18 at 13:19














1












1








1


0





Hi I am currently attempting to learn C and I was wondering if there is a way to attain polymorphism in structures which contain a list of other different type of structures?



An example case of this is as such:



 #include <stdlib.h>
#include <stdio.h>

typedef void (*update_t)(void *);

typedef struct entity entity_t;
typedef struct compA compA_t;
typedef struct compB compB_t;


struct compA

update_t update;
;

struct compB

update_t update;
;

struct entity
update_t update;
int curSize;
void **components;
;


void compA_update(void *c)
printf("updating: componentAn");


compA_t *compA_create()
compA_t *c = malloc(sizeof(compA_t));
c->update = compA_update;
return c;


void compB_update(void *c)
printf("updating: componentBn");



compB_t *compB_create()
compB_t *c = malloc(sizeof(compB_t));
c->update = compB_update;
return c;



void entity_update(void *en)
entity_t *e = (entity_t *)en;
for(int i = 0; i < e->curSize; i++)
//would like to somehow update all the components with one line just iterating through the array but does not seem possible

return;




entity_t *entity_create()
entity_t *e = malloc(sizeof(entity_t));
e->curSize = 0;
e->update = entity_update;
calloc(32, sizeof(void *));
return e;


void add_component(entity_t *e, void *c)
printf("%dn", e->curSize);
e->components[e->curSize] = c;
e->curSize++;
return;





int main(void)

entity_t *e = entity_create();
compA_t *a = compA_create();
compB_t *b = compB_create();
add_component(e, a);
add_component(e, b);

e->update(e);

return 0;



So far my approach to this problem has been solved with void pointer arrays of a tuple structure which contains a enum type which identifies the structure as well as the structure itself and then in a potential update function a fairly ugly switch statement has to be implemented with a case for each specific type.



Is there a better way to do this? As the switch approach will get fairly crazy pretty fast if there are a lot of different types within the array. which means one must explicitly add cases for each type and every case does exactly the same thing, which in this case is call a function pointer named "update".










share|improve this question













Hi I am currently attempting to learn C and I was wondering if there is a way to attain polymorphism in structures which contain a list of other different type of structures?



An example case of this is as such:



 #include <stdlib.h>
#include <stdio.h>

typedef void (*update_t)(void *);

typedef struct entity entity_t;
typedef struct compA compA_t;
typedef struct compB compB_t;


struct compA

update_t update;
;

struct compB

update_t update;
;

struct entity
update_t update;
int curSize;
void **components;
;


void compA_update(void *c)
printf("updating: componentAn");


compA_t *compA_create()
compA_t *c = malloc(sizeof(compA_t));
c->update = compA_update;
return c;


void compB_update(void *c)
printf("updating: componentBn");



compB_t *compB_create()
compB_t *c = malloc(sizeof(compB_t));
c->update = compB_update;
return c;



void entity_update(void *en)
entity_t *e = (entity_t *)en;
for(int i = 0; i < e->curSize; i++)
//would like to somehow update all the components with one line just iterating through the array but does not seem possible

return;




entity_t *entity_create()
entity_t *e = malloc(sizeof(entity_t));
e->curSize = 0;
e->update = entity_update;
calloc(32, sizeof(void *));
return e;


void add_component(entity_t *e, void *c)
printf("%dn", e->curSize);
e->components[e->curSize] = c;
e->curSize++;
return;





int main(void)

entity_t *e = entity_create();
compA_t *a = compA_create();
compB_t *b = compB_create();
add_component(e, a);
add_component(e, b);

e->update(e);

return 0;



So far my approach to this problem has been solved with void pointer arrays of a tuple structure which contains a enum type which identifies the structure as well as the structure itself and then in a potential update function a fairly ugly switch statement has to be implemented with a case for each specific type.



Is there a better way to do this? As the switch approach will get fairly crazy pretty fast if there are a lot of different types within the array. which means one must explicitly add cases for each type and every case does exactly the same thing, which in this case is call a function pointer named "update".







c data-structures polymorphism






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 '18 at 13:11









Simplexity

82




82











  • You can try data polymorphism instead of function pointer. That is, different data produce different behavior, using the same code.
    – JiaHao Xu
    Nov 10 '18 at 13:19

















  • You can try data polymorphism instead of function pointer. That is, different data produce different behavior, using the same code.
    – JiaHao Xu
    Nov 10 '18 at 13:19
















You can try data polymorphism instead of function pointer. That is, different data produce different behavior, using the same code.
– JiaHao Xu
Nov 10 '18 at 13:19





You can try data polymorphism instead of function pointer. That is, different data produce different behavior, using the same code.
– JiaHao Xu
Nov 10 '18 at 13:19













1 Answer
1






active

oldest

votes


















0














You can try data polymorphism instead of function pointer. That is, different data produce different behavior, using the same code.



For example, a simple polymorphic behavior:



#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>

typedef const char* ccstr;

typedef struct animal_attr_t

bool is_body_segmented;
float gill_completeness;
float lung_completeness;
animal_attr_t;

typedef struct species

ccstr name, kingdom, domain;

animal_attr_t animal_attr[0];
species;

void initialize_species_base(species *this, ccstr name, ccstr kingdom, ccstr domain)

this->name = name;
this->kingdom = kingdom;
this->domain = domain;


void initialize_animal_attr(animal_attr_t *this, bool is_body_segmented, float gill_completenss, float lung_completeness)

this->is_body_segmented = is_body_segmented;
this->gill_completeness = gill_completenss;
this->lung_completeness = lung_completeness;


void print_species(species*);

int main(int argc, char *argv)

species *yeast = calloc(sizeof(species), 1);
assert(yeast);

initialize_species_base(yeast, "yeast", "fungus", "eukaryote");

print_species(yeast);

species *dog = calloc(sizeof(species) + sizeof(animal_attr_t), 1);
assert(dog);

initialize_species_base(dog, "dog", "animal", "eukaryote");
initialize_animal_attr(dog->animal_attr, true, 0.0f, 1.0f);

print_species(dog);

free(yeast);
free(dog);


void print_species(species *this)

printf("name = %s, kingdom = %s, domain = %s",
this->name, this->kingdom, this->domain);

if (strcmp(this->kingdom, "animal") == 0)
animal_attr_t *ani_attr = this->animal_attr;
printf(", has %s, %f completeness of gill, %f completeness of lung",
ani_attr->is_body_segmented ? "segmented body" : "unsegmented body",
ani_attr->gill_completeness, ani_attr->lung_completeness);


printf(".n");



yeast and dog is 2 completely different types, yet with species it is expressed in an unified way and print_species has polymorphic behavior.






share|improve this answer






















  • Thanks that is indeed an interesting approach.
    – Simplexity
    Nov 10 '18 at 14:14










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239278%2fways-to-illicit-polymorphic-properties-in-regular-c%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You can try data polymorphism instead of function pointer. That is, different data produce different behavior, using the same code.



For example, a simple polymorphic behavior:



#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>

typedef const char* ccstr;

typedef struct animal_attr_t

bool is_body_segmented;
float gill_completeness;
float lung_completeness;
animal_attr_t;

typedef struct species

ccstr name, kingdom, domain;

animal_attr_t animal_attr[0];
species;

void initialize_species_base(species *this, ccstr name, ccstr kingdom, ccstr domain)

this->name = name;
this->kingdom = kingdom;
this->domain = domain;


void initialize_animal_attr(animal_attr_t *this, bool is_body_segmented, float gill_completenss, float lung_completeness)

this->is_body_segmented = is_body_segmented;
this->gill_completeness = gill_completenss;
this->lung_completeness = lung_completeness;


void print_species(species*);

int main(int argc, char *argv)

species *yeast = calloc(sizeof(species), 1);
assert(yeast);

initialize_species_base(yeast, "yeast", "fungus", "eukaryote");

print_species(yeast);

species *dog = calloc(sizeof(species) + sizeof(animal_attr_t), 1);
assert(dog);

initialize_species_base(dog, "dog", "animal", "eukaryote");
initialize_animal_attr(dog->animal_attr, true, 0.0f, 1.0f);

print_species(dog);

free(yeast);
free(dog);


void print_species(species *this)

printf("name = %s, kingdom = %s, domain = %s",
this->name, this->kingdom, this->domain);

if (strcmp(this->kingdom, "animal") == 0)
animal_attr_t *ani_attr = this->animal_attr;
printf(", has %s, %f completeness of gill, %f completeness of lung",
ani_attr->is_body_segmented ? "segmented body" : "unsegmented body",
ani_attr->gill_completeness, ani_attr->lung_completeness);


printf(".n");



yeast and dog is 2 completely different types, yet with species it is expressed in an unified way and print_species has polymorphic behavior.






share|improve this answer






















  • Thanks that is indeed an interesting approach.
    – Simplexity
    Nov 10 '18 at 14:14















0














You can try data polymorphism instead of function pointer. That is, different data produce different behavior, using the same code.



For example, a simple polymorphic behavior:



#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>

typedef const char* ccstr;

typedef struct animal_attr_t

bool is_body_segmented;
float gill_completeness;
float lung_completeness;
animal_attr_t;

typedef struct species

ccstr name, kingdom, domain;

animal_attr_t animal_attr[0];
species;

void initialize_species_base(species *this, ccstr name, ccstr kingdom, ccstr domain)

this->name = name;
this->kingdom = kingdom;
this->domain = domain;


void initialize_animal_attr(animal_attr_t *this, bool is_body_segmented, float gill_completenss, float lung_completeness)

this->is_body_segmented = is_body_segmented;
this->gill_completeness = gill_completenss;
this->lung_completeness = lung_completeness;


void print_species(species*);

int main(int argc, char *argv)

species *yeast = calloc(sizeof(species), 1);
assert(yeast);

initialize_species_base(yeast, "yeast", "fungus", "eukaryote");

print_species(yeast);

species *dog = calloc(sizeof(species) + sizeof(animal_attr_t), 1);
assert(dog);

initialize_species_base(dog, "dog", "animal", "eukaryote");
initialize_animal_attr(dog->animal_attr, true, 0.0f, 1.0f);

print_species(dog);

free(yeast);
free(dog);


void print_species(species *this)

printf("name = %s, kingdom = %s, domain = %s",
this->name, this->kingdom, this->domain);

if (strcmp(this->kingdom, "animal") == 0)
animal_attr_t *ani_attr = this->animal_attr;
printf(", has %s, %f completeness of gill, %f completeness of lung",
ani_attr->is_body_segmented ? "segmented body" : "unsegmented body",
ani_attr->gill_completeness, ani_attr->lung_completeness);


printf(".n");



yeast and dog is 2 completely different types, yet with species it is expressed in an unified way and print_species has polymorphic behavior.






share|improve this answer






















  • Thanks that is indeed an interesting approach.
    – Simplexity
    Nov 10 '18 at 14:14













0












0








0






You can try data polymorphism instead of function pointer. That is, different data produce different behavior, using the same code.



For example, a simple polymorphic behavior:



#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>

typedef const char* ccstr;

typedef struct animal_attr_t

bool is_body_segmented;
float gill_completeness;
float lung_completeness;
animal_attr_t;

typedef struct species

ccstr name, kingdom, domain;

animal_attr_t animal_attr[0];
species;

void initialize_species_base(species *this, ccstr name, ccstr kingdom, ccstr domain)

this->name = name;
this->kingdom = kingdom;
this->domain = domain;


void initialize_animal_attr(animal_attr_t *this, bool is_body_segmented, float gill_completenss, float lung_completeness)

this->is_body_segmented = is_body_segmented;
this->gill_completeness = gill_completenss;
this->lung_completeness = lung_completeness;


void print_species(species*);

int main(int argc, char *argv)

species *yeast = calloc(sizeof(species), 1);
assert(yeast);

initialize_species_base(yeast, "yeast", "fungus", "eukaryote");

print_species(yeast);

species *dog = calloc(sizeof(species) + sizeof(animal_attr_t), 1);
assert(dog);

initialize_species_base(dog, "dog", "animal", "eukaryote");
initialize_animal_attr(dog->animal_attr, true, 0.0f, 1.0f);

print_species(dog);

free(yeast);
free(dog);


void print_species(species *this)

printf("name = %s, kingdom = %s, domain = %s",
this->name, this->kingdom, this->domain);

if (strcmp(this->kingdom, "animal") == 0)
animal_attr_t *ani_attr = this->animal_attr;
printf(", has %s, %f completeness of gill, %f completeness of lung",
ani_attr->is_body_segmented ? "segmented body" : "unsegmented body",
ani_attr->gill_completeness, ani_attr->lung_completeness);


printf(".n");



yeast and dog is 2 completely different types, yet with species it is expressed in an unified way and print_species has polymorphic behavior.






share|improve this answer














You can try data polymorphism instead of function pointer. That is, different data produce different behavior, using the same code.



For example, a simple polymorphic behavior:



#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <assert.h>

typedef const char* ccstr;

typedef struct animal_attr_t

bool is_body_segmented;
float gill_completeness;
float lung_completeness;
animal_attr_t;

typedef struct species

ccstr name, kingdom, domain;

animal_attr_t animal_attr[0];
species;

void initialize_species_base(species *this, ccstr name, ccstr kingdom, ccstr domain)

this->name = name;
this->kingdom = kingdom;
this->domain = domain;


void initialize_animal_attr(animal_attr_t *this, bool is_body_segmented, float gill_completenss, float lung_completeness)

this->is_body_segmented = is_body_segmented;
this->gill_completeness = gill_completenss;
this->lung_completeness = lung_completeness;


void print_species(species*);

int main(int argc, char *argv)

species *yeast = calloc(sizeof(species), 1);
assert(yeast);

initialize_species_base(yeast, "yeast", "fungus", "eukaryote");

print_species(yeast);

species *dog = calloc(sizeof(species) + sizeof(animal_attr_t), 1);
assert(dog);

initialize_species_base(dog, "dog", "animal", "eukaryote");
initialize_animal_attr(dog->animal_attr, true, 0.0f, 1.0f);

print_species(dog);

free(yeast);
free(dog);


void print_species(species *this)

printf("name = %s, kingdom = %s, domain = %s",
this->name, this->kingdom, this->domain);

if (strcmp(this->kingdom, "animal") == 0)
animal_attr_t *ani_attr = this->animal_attr;
printf(", has %s, %f completeness of gill, %f completeness of lung",
ani_attr->is_body_segmented ? "segmented body" : "unsegmented body",
ani_attr->gill_completeness, ani_attr->lung_completeness);


printf(".n");



yeast and dog is 2 completely different types, yet with species it is expressed in an unified way and print_species has polymorphic behavior.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 '18 at 14:07

























answered Nov 10 '18 at 14:02









JiaHao Xu

599315




599315











  • Thanks that is indeed an interesting approach.
    – Simplexity
    Nov 10 '18 at 14:14
















  • Thanks that is indeed an interesting approach.
    – Simplexity
    Nov 10 '18 at 14:14















Thanks that is indeed an interesting approach.
– Simplexity
Nov 10 '18 at 14:14




Thanks that is indeed an interesting approach.
– Simplexity
Nov 10 '18 at 14:14

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239278%2fways-to-illicit-polymorphic-properties-in-regular-c%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)