Ways to illicit polymorphic properties in regular C?
up vote
1
down vote
favorite
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
add a comment |
up vote
1
down vote
favorite
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
You can try data polymorphism instead of function pointer. That is, different data produce different behavior, using the same code.
– JiaHao Xu
Nov 10 at 13:19
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
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
c data-structures polymorphism
asked Nov 10 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 at 13:19
add a comment |
You can try data polymorphism instead of function pointer. That is, different data produce different behavior, using the same code.
– JiaHao Xu
Nov 10 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 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 at 13:19
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
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.
Thanks that is indeed an interesting approach.
– Simplexity
Nov 10 at 14:14
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
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.
Thanks that is indeed an interesting approach.
– Simplexity
Nov 10 at 14:14
add a comment |
up vote
0
down vote
accepted
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.
Thanks that is indeed an interesting approach.
– Simplexity
Nov 10 at 14:14
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
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.
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.
edited Nov 10 at 14:07
answered Nov 10 at 14:02
JiaHao Xu
549215
549215
Thanks that is indeed an interesting approach.
– Simplexity
Nov 10 at 14:14
add a comment |
Thanks that is indeed an interesting approach.
– Simplexity
Nov 10 at 14:14
Thanks that is indeed an interesting approach.
– Simplexity
Nov 10 at 14:14
Thanks that is indeed an interesting approach.
– Simplexity
Nov 10 at 14:14
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
You can try data polymorphism instead of function pointer. That is, different data produce different behavior, using the same code.
– JiaHao Xu
Nov 10 at 13:19