Jump to content
  • Advertisement
Sign in to follow this  
RaptorZero

Function pointer problem

This topic is 5467 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey, this is a classic problem wich I can't solve, here it is:
class CFirstClass
{
    ...
    void Function(void);
    ...
}

class CSecondClass
{
    ...
    void (CFirstClass::*pFunc)(void);
    ...
}

I have theese two (hypotetical) classes, one has a function and the other a function pointer to that function then inside CFirstClass I set the function pointer: pClassManager->CSecondClassInstance.pFunc = Function; and inside CSecondClass I call this function pFunc(); this causes "error C2064: term does not evaluate to a function" (VC++)... what am I doing wrong??? I've spent some time searching this formu but non of the topics I've seen could solve my problem, can anyone help me? If it matters (I don't think it does) CSecondClass is a derived class... If you had the pacience to read all the topic till here thanx a lot! :) <edited: I've tried to put some source tags>

Share this post


Link to post
Share on other sites
Advertisement
I'm not entirely sure (I've never tried doing what you're doing!) but you might need to declare the method you're calling "static" for this to work.


Ryan

Share this post


Link to post
Share on other sites
Hello,

it seems that you are trying to use the C++ pointer to members as some classical C pointer to functions. It do not work that way. When you call pFunc, it must know the context in which it must executed - namely: the this pointer. Therefore, the call statement must refer to an instance of the object.

The C++ FAQ Lite has good informations about pointers to members, and specifically this line of code:


#define CALL_MEMBER_FN(o,pfn) ((o).*(pfn))


You code then become


CFirstClass someInstance;
CSecondClass sc;

// ...

CALL_MEMBER_FN(someInstance, sc.pFunc)();


I know this is rather strange and not intuitive at all. But you'll have to go that way if you want to use pointers to members.

Regards,

Share this post


Link to post
Share on other sites
I hate when that happens!!!
I'm with a problem for some days, I've googled it and searched this forum, then someone comes and says "try this" and everything is solved in one single line of code! :)

Thanks a lot for all the answers, specialy Emmanuel Deloget

I used that define

#define CALL_MEMBER_FN(o,pfn) ((o).*(pfn))

and everything went just fine :)
thank you very much for everything, problem solved...

cya around...

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
Just want to reiterate that function-pointer.org is an invaluable resource.


Just as the C++ FAQ Lite :) I notice that the Forum FAQ do not contain any link - but only informations about good and bad people behavior. Maybe the forum moderator could update the FAQ to include such kind of information ?

Regards,

Share this post


Link to post
Share on other sites
You can make the pointer a pointer to a function taking a pointer to an instance followed by the rest of the parameters. You have to use some funky casts or a union to actually assign the address of the function to the pointer. That is a strict answer to your question. You should not do that though. You can also make it a static function. Not as gross a violation of good programming practices, but still a poor design.

In practice you want a pointer to a function because you need to call one of many functions. The means C++ provides to do that is inheritance and virtual functions. You assign a variable of type pointer to instance of the base class the address of an instance of a derived class. You give the function the same name and parameters in both the base and derived class and declare them virtual. When you call that function through a variable of type pointer to the base class it will call the function in whatever you are actually pointing to.

How it does this is through function pointers, but it is all transparent to you. When you have virtual functions in a class the first item in the data storage for that class is a pointer to a table of pointers to functions. A function declared virtual in the base is the same offset in that table in all the derived classes as well, but the derived classes all have their own table of pointers. That lets them replace a pointer with a pointer to their own function. So when you call Base->Function that means call the function pointed to by a specific entry in the table of function pointers. Which table of function pointers depends upon what it is actually an instance of. It could be Base::Function or Derived::Function depending upon whether you are pointing at an instance of Base or Derived.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!