Originally Posted by
tempster09
Code:
mytype1->function(); //no parameter, can it find out which variable i want to manipulate?
If there is only one possible answer, then yes it can. If there is more than one possibility, what do you think the answer is here?
BTW, if you are doing this to mimic method calls, DON'T. That is not the purpose of function pointers; function pointers are for situations where you may substitute one function for another. Using them just so you can use method syntax is stupid, because:
1) this is less efficient than calling a function directly -- which is why I said before, adherring to an OO model just for it's own sake will lead to unnecessary inefficiencies such as excess memory usage, excessive indirection, etc.
2) the code ends up being less comprehensible (not more) in exact relation to it's decrease in efficiency, because you have actually given the processor unnecessary instructions to do unnecessary things, and those unnecessary directives are just clutter.
Once again, the normative and quite common approach to OO in C is to make the "methods" external but name them in relation to the "class" they operate on:
Code:
typedef struct banana;
void banana_dothis(banana *ptr);
void banana_dothat(banana *ptr);
Yes, unfortunately that means you cannot have a instance of banana and do this:
Code:
banana realbanana;
realbanana.dothat();
Instead, you must resort to the arcane and ridiculous
Code:
banana_dothat(&realbanana);