Chapter 5. Semantics of Construction, Destruction, and Copy
Consider the following abstract base class declaration:
class Abstract_base { public: virtual ~Abstract_base() = 0; virtual void interface() const = 0; virtual const char* mumble () const { return _mumble; } protected: char *_mumble; };
Do you see any problems? Although the class is designed to serve as an abstract base class (the presence of a pure virtual function disallows independent instances of Abstract_base to be created), the class still requires an explicit constructor in order to initialize its one data member, _mumble
. Without that initialization, a local object of a class derived from Abstract_base will have its instance of _mumble
uninitialized. For example:
class Concrete_derived ...
Get Inside the C++ Object Model now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.