Reputation: 3
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 (i.e. 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?
Upvotes: 0
Views: 1043
Reputation: 2916
The following seems to compile:
MyClass()
{
f = &MyClass::MyFunc1;
}
void MyFunc1()
{
// ...
}
void MyFunc2() const
{
MyClass* mc = const_cast<MyClass*>(this);
(mc->*f)();
}
Upvotes: 0
Reputation: 3172
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.
Upvotes: 2
Reputation: 136266
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
.
Upvotes: 1