user4324675
user4324675

Reputation:

Function pointer from other class C++

I have a problem to call a function pointer in map in c++.

this is my function pointer type:

typedef int (AEvent::*setFunction)();

this is my map :

std::map<const std::string, setFunction *>      _actions;

so i find my function like this:

auto mem = this->_actions.find("SHOOT")->second;

and now how can i call the function ?

Thanks in advance for your reply.

Upvotes: 0

Views: 77

Answers (1)

Dean Seo
Dean Seo

Reputation: 5693

Suppose that you have your class hierarchy as follows:

class AEvent
{
public:
    virtual int Bar() = 0;
};

class Derived : public AEvent
{
public:
    virtual int Bar() override
    {
        // Return something.
        return 100;
    }
};

You can invoke a pointer to member function that is virtual as follows:

AEvent * my_object = GetDerived();

auto func = this->_actions.find("SHOOT")->second;

(my_object->*func)() // Invoke 'func'

If you invoke a pointer to member function, keep in mind that you are responsible for providing this pointer correctly, which in this case happens to be my_object

Upvotes: 1

Related Questions