简体   繁体   中英

Save a pointer to member function of derived class

I am looking for a way to save a pointer to a member function of a derived class.

For exemple:

class A
{
public:
    typedef void (A::*FunctionP) (int a);
};

class B : A 
{
public:
    void Test(int a)
    {
        //Do stuff
    }
    B()
    {
        FunctionP theFunc = &B::Test;
    }

};

The following code dose not compile.. Is there another way to do this? (Using template's maybe, or boost)

(Btw class A is an abstract class witch one of its implementations is invoking functions saved as FunctionP)

Thank you

Just use a static_cast :

FunctionP theFunc = static_cast<FunctionP>(&B::Test);

Coliru: http://coliru.stacked-crooked.com/a/2cfed4926aed43db

Still, it might be even better to use std::function and std::bind , depending on your needs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM