C++ abstract class destructor -
C++ abstract class destructor -
is practice (and possible) create abstract class using pure virtual destructor in parent class?
here's sample
class abstractbase { public: abstractbase () {} virtual ~abstractbase () = 0; }; class derived : public abstractbase { public: derived() {} virtual ~derived() {} }; otherwise how can create abstract class if attributes , constructors of derivatives class same while other method totally different?
having pure virtual destructor in base of operations class practice, possible and, in cases, desirable. instance, if rely on rtti dispatch messages subclass object attempting dynamic_cast on pointer base of operations class, may need no methods in base of operations class except destructor. in case, create destructor public virtual.
since have no other virtual methods except destructor, have create pure in order prevent creation of objects of base of operations class. here, crucial provide empty body destructor (even though "=0"!)
struct abstractbase { virtual ~abstractbase() = 0; } abstractbase::~abstractbase() { } having body allows creation of subclass objects (provided subclass defines destructor , not set pure).
c++ abstract-class destructor
Comments
Post a Comment