PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

c++ question

Hi,
my problem is as follows:
I have a class X from which i plan to derrive unknown number of other classes Y1, Y2, Y3...(each will have only one instance) then there is another class Z unknown number of objects of which are declared in Ys but their addresses are saved in a link list declared in X so that X has access to all Z objects in corresponding Y. I want to pass a function pointer pointing to a method of Y as a parameter to Z object and then retrieve it from Z in X and call it there.
The goal is to give ability to X to call unspecified number of methods of Y.
(each Y will have different number of methods needed)
is this even possible to do?
can a parent class call methods of its child using function pointers?
Thanks.
Let me restate your question in simpler terms.

You want an instance of X to be able to call a non-static method on a specific instance of Y. This will require knowing the class of Y (Y1, Y2, etc.) and also the object (the "this" which gets passed to the non-static method).

Is that correct?

You can pass the method as a pointer to member function, and the object as a pointer to an instance. However, both require knowing the class. So the problem becomes, how does X know the class of Y?

Now think a minute about how virtual methods work. The function to call is decided at compile time, and the implementation (whether it is overridden or not) is decided at runtime.

So maybe you should redesign so that the methods you are interested in calling are in X. Then you pass around pointers to methods in X, and pointers to X objects. Now X can call the appropriate X method on the X instance.

It's just in your case, the X instances called are actually Y children of X, and override the methods of X.

You see what is happening? The functions are all in X but the implementations are called in Y. Is this what you want?
thanks mlepage,

X will not have an instance only Ys will have instances, one instance for each Y

X incorporates lots of routine stuff that all Y have to have and I guess your suggestion would work except my goal is to have the number of Y methods that are to be called in X implementation unspecified (Y will save the pointers to those methods to a link list declared in X); the number of Y methods will vary widely for each Y

I think that instead of making those methods members of Y, I will simply make them standart C functions such that they will be friends of corresponding Y so that I'll have a complete access to Y members inside those functions; then X should have no problem calling them using standart C function pointers.

I guess this sort of brakes the encapsulation, but it should do the job well...

Again, thanks for your reply,
Cheers.
Whether you use a pointer to regular function or pointer to member function will make no difference.

If you want your functions to have access to your Ys, to do things with them, they will have to both know the class and have an instance of that class passed to them.

Whether you do this manually using pointer to regular functions, or automatically using pointer to member function, is just syntax. The pointer to member function syntax is cleaner.

If you have Stroustrup's 3rd edition (you should), it's covered in section 15.5.

Declare a type alias like this:

Code:

typedef void (Y1::* Y1Handler)();


Now you can store your pointer to member function in a variable of that type:

Code:

Y1Handler myY1Handler;


Now, let's say you have a reference to an object of class Y1:

Code:

Y1& myY1;


You call the method like this:

Code:

(myY1.*myY1Handler)();
the function itself will know Y(and the particular instance that the function will work with, that instance will be global), but X won't (I plan to use X in many applications); so if I understand correctly,
if I make the function a member of Y, X will need to know Y in order to call that function(this won't work for me), but if I make the function as a standart C function(the function will know Y and have the instance) with a certian declaration syntax, X will need to know that declaration syntax and not Y(this is what I'm looking for).
I guess so.

It's hard to keep track of al this. Maybe try it with regular function pointers.
ok, it seems to work just fine,
mlepage thanks for your replies, I really appreciate it...