I now have an issue with calling functions that are defined only in
Quote:
BaseClass and not ReallyBaseClass. If I have pointers to
ReallyBaseClass I still have to perform a cast to BaseClass to perform
those function calls. How then do I cast to have the correct Type
associated with it?
If you want to call method (let's say named method()), you can either
(a) define such function as virtual in ReallyBaseClass
(b) if you know, that certain pointer to ReallyBaseClass points to
instance of Base<SomeType>, you can typecast it this way:
ReallyBaseClass* p = // get it from somewhere
Base<SomeType>* pp = dynamic_cast<Base<SomeType>*>(p);
if (pp == 0)
{
// p was not pointer to instance of Base<SomeType(or class
derived from it)
}
else
{
// call method of Base<SomeType>
pp->method();
}
I prefer variant (a), because I do not need to perform ugly typecasts.
In ReallyBaseClass I usualy define method() to always fail to be able
to detect, that it was called for class, where it is nonsense:
class ReallyBaseClass
{
public:
// ... constructors and more methods
// If you are using exceptions
virtual void method()
{
throw SomeGeneralException;
}
// If you do not use exceptions
virtual void method()
{
std::cerr << "ReallyBaseClass::method() called\n";
exit(1);
}
};