smallB
smallB

Reputation: 17120

Pointer to member function - doesn't want to work

in this short code:

class X
{
private:
    class Y
    {

    public:
        typedef void (X::* ptr_to_mem)();
        Y(X* parent,ptr_to_mem ptr):parent_(parent),ptr_(ptr)
        {}
        void run()
        {
            parent_->*ptr_();//at this line I'm getting an error
        }
    private:
        X* parent_;
        ptr_to_mem ptr_;
    };

public:
    void some_fnc()
    {
        cout << "some_fnc";
    }

    void another()
    {
        Y y_(this,&X::some_fnc);
        y_.run();
    }

};

error:

error: must use '.*' or '->*' to call pointer-to-member function in '((X::Y*)this)->X::Y::ptr_ (...)', e.g. '(... ->* ((X::Y*)this)->X::Y::ptr_) (...)'

Upvotes: 2

Views: 139

Answers (1)

Mat
Mat

Reputation: 206699

Add an extra pair of parens:

(parent_->*ptr_)();

See C++FAQ lite 33.6.

Upvotes: 6

Related Questions