Jump to content
  • Advertisement
Sign in to follow this  
Error98

Member ponter to member function problem *SOLVED*

This topic is 4844 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

Ehhh yet another problem with member pointers to member functions

class Engi
{
   private:
     void D1(int a, int b);
     void D2(int a, int b); 
   public:
     void (Engi::*ptrD)(int a, int b);
     void Set();
     void Start();
}

Engi::Set()
{
  ptrD = &D1; // No problem here
}

Engi::Start()
{
  ptrD(10,10); // And here error pops "term does not evaluate to a function taking 2 arguments" i can only use ptrD with out any args!! which is pointless :/

  ptrD(); // No problem here (wtf?)
}

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)
{
  Engi eng;
  eng.Set();
  eng.ptrD(10,10); // same error: "term does not evaluate to a function taking 2 arguments"
  //  eng.*ptrD(10,10) results in "identifier not found, even with argument-dependent lookup"

}


Any one know whats wrong with this code? If i won't find solution soon ... i'll simply explode :[ [Edited by - Error98 on September 4, 2005 3:43:13 PM]

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
I am not a C++ expert, but I beleive you can only take a pointer to a class member if it declared as static.

Mr. Creamy.

Share this post


Link to post
Share on other sites
class Engi
{
private:
void D1(int a, int b);
void D2(int a, int b);
public:
void (Engi::*ptrD)(int a, int b);
void Set();
void Start();
};

void Engi::Set()
{
ptrD = &Engi::D1;
}

void Engi::Start()
{
(this->*ptrD)(10,10);
}

int WINAPI WinMain(HINSTANCE hinstance, HINSTANCE hprevinstance, LPSTR lpcmdline, int ncmdshow)
{
Engi eng;
eng.Set();
(eng.*eng.ptrD)(10,10);

}





There were lots of syntax errors even not concerning the pointer to members such as missing semicolons and return types. Other than those, you have to remember that the only valid way to take the address of a member function is by the form &your_class_type::your_member_function. Note that you always must do that even from the class's scope (if your compiler allows you to not use that syntax, then it is the fault of the compiler). Also, you can't just call a member function via a member function pointer without specifying what object instance you are using with it. Just because the pointer to member function is a datamember doesn't mean that it automatically always uses the encapsulating class instance (as it shouldn't). You must connect it with an object using either the .* operator or the ->* operator, though remember that operator() is stronger so you have to encapsulate the expression in parenthesis prior to completing the call.

Share this post


Link to post
Share on other sites
After few hours of fighting with this pointers i'm hardly stoping my self from throwing my display through the window .... i even calculated how long it will fall before it finaly crashes!! 1,8069310999592651872554356596748sec

Quote:
I am not a C++ expert, but I beleive you can only take a pointer to a class member if it declared as static.


I'll jump right after my display if it's true ... so better not! :D

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!