different max_structs – using the same method – polymorphism

Oct 9, 2012 at 12:46pm

different max_structs – using the same method – polymorphism

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.

#64272
Oct 9, 2012 at 2:03pm

Hi,

Could you be more explicit (please join code) since i really do not understand what you try to achieve ; I guess that what you expect to do is not polymorphism ; but just how to design a library usable by several externals ? Tell me if i’m wrong.

#231760
Oct 9, 2012 at 2:56pm

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.?

#231761
Oct 9, 2012 at 3:33pm

Hi,

I will not argue, i’m not an expert in polymorphism ; anyway i can see now what you want to do ; i have a vague idea but i need time to investigate ;-) Somebody in the audience ?

#231762
Oct 9, 2012 at 4:09pm

Hi,

One way is to define a custom object structure to encapsulate the max t_object structure (as in the quick flop external i join below to illustrate the idea).

typedef struct _ob {
 t_object o;
 long key;
 } t_ob;

typedef struct _flop {
t_ob ob;
void *outlet;
} t_flop;

HTH

Attachments:
  1. flop.c
#231763
Oct 9, 2012 at 6:02pm

Hi,

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

#231764
Oct 10, 2012 at 10:53am

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

#231765

You must be logged in to reply to this topic.