Let\'s say I have a Base class and several Derived classes. Is there any way to cast an object to one of the derived classes without the ne
You can do this using dynamic_cast, e.g:
if ( Derived1* d1 = dynamic_cast(object) ) {
// object points to a Derived1
d1->foo();
}
else if ( Derived2* d2 = dynamic_cast(object) ) {
// object points to a Derived2
d2->bar();
}
else {
// etc.
}
But as others have said, code such as this can indicate a bad design, and you should generally use virtual functions to implement polymorphic behaviour.