Sign in to follow this  
Windhawk

Problem calling pointer to member function (Solved)

Recommended Posts

I have a menu class, which contains a vector of buttons (another class). Each button also has assigned to it a pointer to a member function of the menu class (the effect of the button). These are then called according to which button is clicked / pressed. But I can't seem to manage to call the member functions from the pointers correctly (code won't compile). Here's the menu class showing relevant bits:
class cMenu {
    private:
        void display();
        void addButton(string text, mePointer mePtr);
        bool doButton(vector<button>::const_iterator bi);
        
        void meNewGame();
        void meLoadGame();
        void meSettings();
        void meReturn();
        void meQuit();
        
        vector<button> mButtons;

};



and here's relevant parts of the button class with the typedef, to make valid pointers to the cMenu::meFoos():
typedef void (cMenu::*mePointer)();
class button {
    public:
        friend class cMenu;
    private:
        string text;
        mePointer mePtr;
};



Buttons are added to the array like so:
        addButton("Return", &cMenu::meReturn);
void cMenu::addButton(string text, mePointer mePtr) {
    button temp;
    SDL_Color c;
    
    temp.text = text;
    temp.mePtr = mePtr;
    
    mButtons.push_back(temp);
}

//and for ref
void cMenu::meReturn() {
    menu.finish();
}



I try to call the pointed to functions like so:
void cMenu::display() {
    for (vector<button>::const_iterator i=mButtons.begin(); i!=mButtons.end(); ++i) {
        if (doButton(i))
            (i->*mePtr)();
    }
}



This gives me the compiler error that "`mePtr' undeclared (first use this function) ". I've tried messing around with the call, but I can't seem to get it right. Am I doing something fundamentally wrong here? [Edited by - Windhawk on December 14, 2006 9:21:51 PM]

Share this post


Link to post
Share on other sites
Quote:
(i->*mePtr)();

operator ->* will only work if i is a button* but it is instead a iterator of a vector of buttons and so you have to use ((*i).*mePtr)()

Quote:
Try this

i->mePtr();


To my knowledge that wont work as mePtr still needs to be dereferensed the problem is that SC++L classes dont overload the ->* operator

Share this post


Link to post
Share on other sites
Quote:
Original post by Windhawk
Quote:
Original post by LessBread
Try this

i->mePtr();


"must use .* or ->* to call pointer-to-member function..."


You want:

(this->*(i->mePtr))();


Hint: It will bitch about mePtr being private in this context.
Edit: As Julian90 points out, ->* is not implemented by iterators.
Edit: As uavfun in IRC points out, mePointer is a cMenu::*, not cButton::*

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this