简体   繁体   中英

Calling non-const member function pointer from const member function

I have the following code:

class MyClass;
typedef void(MyClass::*func_ptr)();
class MyClass
{
public:

MyClass()
{
    f = &MyFunc1;
}

void MyFunc1()
{
    // ...
}

void MyFunc2() const
{
    (this->*f)();
}

func_ptr f;

};

If I try to compile, it fails because of MyFunc2() which is a const method trying to call the function pointer of type func_ptr which is non-const.

I'm trying to figure out the best way to cast this. I can use a standard C style cast:

    typedef void(MyClass::*func_ptr2)() const;
    func_ptr2 f2 = (func_ptr2)f;
    (this->*f2)();

But I'd prefer to use a C++ cast (ie static_cast, reinterpret_cast, or const_cast). I got this to compile with reinterpret_cast but I can't get it working with const_cast. I thought const_cast was mean't to be used for this case? Also, is there a cleaner way to do this without having to create another typedef?

The typical solution would be (const_cast<MyClass*>(this)->*f)(); .

This is legal as long as the MyClass instance has been created non-const. Else, this would invoke undefined behavior.

There are a couple of issues in this code.

 f = &MyFunc1;

Is not well-formed C++ because taking an address of a non-static member function requires a fully qualified name:

 f = &MyClass::MyFunc1;

this is MyClass const* in a const member function. You may need to cast away that const :

(const_cast<MyClass*>(this)->*f)();

A better solution may be to rethink the design to avoid fighting the type system with const_cast .

The following seems to compile:

MyClass()
{
    f = &MyClass::MyFunc1;
}

void MyFunc1()
{
    // ...
}

void MyFunc2() const
{
    MyClass* mc = const_cast<MyClass*>(this);
    (mc->*f)();
}

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