Anders Wegge Jakobsen wrote in a message to Pierre Phaneuf:
AWJ> Where exactly are one supposed to use dynamic_cast?
dynamic_cast is used to reliably downcast a pointer from a base class to a
derived class. If the cast is valid, the cast will return the pointer. If the
cast is not valid, dynamic_cast returns 0. eg:
class Base {};
class Derived : public Base;
Base * pd = new Derived;
Base * pb = new Base;
Derived* dp = dynamic_cast(pd);
Derived* bp = dynamic_cast(pb);
now bp will be 0 and dp will be unequal to 0. This is a way to test reliably
if an object belongs to a certain derived class. For this to work you will
have to make sure that Base has at least one virtual member function.
PP> #define abstract = 0
PP> So you just do that:
PP> virtual void fn() abstract
AWJ> Personally, I think this way is a kludge which should never
AWJ> have been legal. The same effect is much cleaner obtained by
AWJ> declaring the constructor in the protected: section of the
AWJ> class.
Pure virtual functions not have anything to do with constructors. ctors are
NOT allowed to be virtual. You are mixing up two concepts here.
Pure virtual functions are used to prohited instantiating a class.
Constructors are used to create a class.
Making a constructor protected does indeed prohibit public users to
instantiate that class, but the class itself still can do it (even 'by
accident'). Protected constructors also do not enforce derived classes to
provide some functionality. A derived class MUST implement an inherited pure
function otherwise the class is not concrete (i.e. instantiable).
mvg/wr
--- timEd/2 1.01.g3+
---------------
* Origin: LightHouse BBS ==> I am a H.U.G.O. Member ! (2:285/324.3)
|