Item 33. Abstract Foundation Imitation Show (Manufacturing Abstract Bases)

xiaoxiao2021-03-05  23

Item 33. Manufacturing Abstract Bases

Abstract base categories describe an abstract concept of a problem that cannot be instantiated. Class abc {public: virtual ~ abc (); virtual void Anoperty () = 0; // pure // ...}; a pure virtual function in a class becomes an abstract class, the compiler guarantees that ABC cannot Instantiate.

Sometimes a class does not have a candidate pure virtual function, but I want it to be instantiated, how to do?

1. Constructor and copy constructor declaration to protect member class abc {public: virtual ~ abc (); protected: abc (); abc (const abc&); //...}; Class D: public ABC {/ / ...};

2, declare the destructor as a pure virtual function and provide its implementation class ABC {public: Virtual ~ abc () = 0; // ...}; // ... ABC :: ~ abc () {. ..}

3. When a class does not have a virtual function, there is no need to explicitly declare the constructor, declare the non-false argument function as a protective class abc {protected: ~ abc (); public: // ...} ;

转载请注明原文地址:https://www.9cbs.com/read-35248.html

New Post(0)