To me it looks perfectly safe to cast a void(Derived::*)()
to a void(Base::*)()
, like in this code:
#include <iostream>
#include <typeinfo>
using namespace std;
struct Base{
void(Base::*any_method)();
void call_it(){
(this->*any_method)();
}
};
struct Derived: public Base{
void a_method(){
cout<<"method!"<<endl;
}
};
int main(){
Base& a=*new Derived;
a.any_method=&Derived::a_method;
a.call_it();
}
But the compiler complains about the cast at a.any_method=&Derived::a_method;
. Is this a roadblock to prevent subtle programming errors, or just something to make life easier for compiler writers? Are there workarounds to let the Base
class have a pointer to member functions of Derived
without type knoweledge (that is, I cannot make Base
a template with template argument Derived
).
What happens if your
Derived::a_method()
attempts to use a data member only present inDerived
, not inBase
, and you call it on aBase
object (or an object derived fromBase
but not related toDerived
)?The conversion the other way around makes sense, this one doesn't.
I imagine this can be somewhat surprising. However it makes sense if you think about it.
For a cast between two types to be automatic, the following relation should hold: any instance of the first type should be representable in the second.
For example, if
d
is an instance ofDerived
, then it can be automatically cast as aBase&
because any instance ofDerived
is also an instance ofBase
. This is inheritance.Now, when it comes to pointer to member-functions, the relation is actually reversed. It will seem obvious that any method of
Base
exists for an instance ofDerived
but the reverse is not true. After all the whole point of deriving is to add new functionality more often that note.Another way of visualizing this is to use free-functions.
this
is just an implicit parameter in regular function, if we make it explicit we get:Now, if I have two instances
d
of typeDerived
andb
of typeBase
then:Base@call_it(d)
makes senseDerived@a_method(b)
is a compilation errorThe latter could be
Derived@a_method(dynamic_cast<Derived&>(b))
, but this introduces a runtime check to actually verify the property. Statically it is not decidable.No, it's potentially dangerous.
A derived class function can use all the derived class properties of
*this
. A pointer to base class function can be called on any base class instance, even those that are not of the derived type.Accessing the derived class properties of an instance that isn't a derived class isn't going to work so casting a pointer to derived class function to a pointer to base class pointer is correctly not allowed.
On the other hand, casting a pointer to base class function to a pointer to derived class function is safe and legal.
You need to use a
std::function<void()>
. This can be any member of any class, a lambda, a free function, a function object, whatever you need, which is super convenient.Here you can see that the actual implementation of
any_method
is totally abstracted fromstruct Base
, and I can supply a function object that does anything, at all- including conveniently calling a Derived method.