Base class's function pointer points to child class's member function?
I know it sounds awefully confusing, I have a base template class which has a function pointer, a child class(which is no longer a template class) needs to use that function pointer to point to a child class's member function, and I get all kinds of errors.. Did I violate some universal law of C++? here is the pseudo code:
template <class T>
class Base{
public:
typedef void (Base<T>::*fptr) (char in);
void funcA(fptr FuncToCall){
FuncToCall('a');
}
...
};
class Child:public Base<char>{
开发者_运维问答public:
void funcToCall(){...}
void funcB(){
funcA(funcToCall);
}
}
Here is the error message I got:
error C2664: 'Base< T >::funcA' : cannot convert parameter 1 from 'void (__thiscall Child::* )(char)' to 'void (__thiscall Base< T >::* )(char)'
Your code is invalid.
Firstly, in C++ to create a pointer to a member function you always have to use operator &
and a qualified-name of the member, which means that your call to funcA
should look as follows
funcA(&Child::funcToCall); // close, but not yet, see "secondly"
Secondly, member pointers are contravariant. If want to initialize a base-member pointer object with a derived-member pointer value, you have to use an explicit static_cast
funcA(static_cast<fptr>(&Child::funcToCall));
Note that this is a valid, but potentially unsafe conversion, since it is now your responsibility to ensure that the actual object used on the left-hand side of the call (see "thirdly") has that member.
Thirdly, in order to call a function through such a pointer, you have to supply a concrete object on the left-hand side, so the call in funcA
should look as follows (I assume that you want to call it for *this
object)
(this->*FuncToCall)('a');
Fourthly, your funcToCall
is supposed to have a parameter. Where is it?
精彩评论