I have a particular problem I want to solve, I'm not sure if it's possible as I can't find any information or examples of it being done. Basically, I have:
class ParentObject {};
class DerivedObject : public ParentObject
{
void myFunction(){}
};
class OtherDerivedObject : public ParentObject
{
void myOtherFunction(){}
};
and want a function pointer to ParentObject::* and have it able to take functions from either derived class. My reason for wanting to do so, I have another class
class functionRegistry
{
std::map<string, *functionPoint> functionMap;
}
and each object (ideally in ParentObject but can do individually in the derived objects if necessary) have an instance of a functionRegistry, and I need functionPoint to be able to point to functions in objects of either type DerivedObject or OtherDerivedObject.
Thanks in advance
All you need is a
static_cast
to populate the map with the correct type.As this is allowed by the standard:
[expr.static.cast/12] - §5.2.9¶12
But while this is allowed, you must be very careful to make sure you apply the pointer to member on an object with the correct dynamic type.