different max_structs - using the same method - polymorphism

    Oct 09 2012 | 12:46 pm
    Hi, to achieve what is hinted in the title would save me a lot of code duplication. So here's the problem:
    All of my externals contain at least these ingredients
    typedef struct { t_object standart_header; int key; }t_specific_struct;
    In my case there's one class/file containing the functions (which need the key to work) and there are "sub"files which import the header of that topfile.
    Now I would like to use the same methods of that topfile for different max_structs which depend on that key, knowing that all structs will have that key.
    I tried using
    typedef struct { t_object standart_header; int key; }t_generic_struct;
    in the topfile because I thought it might work having the same structure but didn't.

    • Oct 09 2012 | 2:56 pm
      Hi, sorry for being a bit imprecise.
      -----------pseudo-code-------------------- //specific part
      #import "test_super.h" int main (int argc, const char * argv[]) { ... class_addmethod(c, (method)test_method, "test", 0); }
      // I'd like to avoid this and use "test_method_super" directly void test_method(t_specific_struct *x) { test_method_super(x->key); }
      --------------------------------------------------- //generic part - the test_super.c file - how I want it to work
      void test_method_super(t_generic_struct * x) { do s.th with the key of x; //not possible at the moment } -------------------------------------------------- If a method interface allows different types as parameters, isn't that (parametric )polym.?
    • Oct 09 2012 | 6:02 pm
      this is one of the reasons why I develop my externals in C++ instead of C. Just search the net for 'object oriented programming'. If in C++, you should check the part where they talk about 'virtual functions'.
      HTH, Ádám
    • Oct 10 2012 | 10:53 am
      Hi, I got it working with something like Nicolas proposed. Thanks a lot!
      This is how:
      I made a struct that contains t_object and the key
      typedef struct _ob { t_object o; int key; } t_things_in_common;
      Using t_ob as the generic type directly didn't work. All values received were 0.
      But making another struct
      typedef struct { t_things_in_common ob; }t_generic;
      and then using this type in the super method works!
      void test_method_super(t_generic * x) { do s.th with the key of x; //works now }
      Every object that contains the t_things_in_common struct in first place can use functions which have t_generic as a parameter type.
      typedef struct { t_things_in_common ...more ingredients; }t_specific;
      Again thanks for helping out, also for the advice having a look at c++. Greetings, Alex