Item 29. Virtual Constructionors and prototype & item 30. Factory Method

xiaoxiao2021-03-05  28

Item 29. Virtual Constructionors and prototype

Virtual Constructors? C No Virtual Construction This concept. But in some cases this feature, that is, an object that can be created in the same dynamic type with the pointer during polymorphism. Since it is a polymorphism, we must consider the virtual function. Is it possible to call the constructor in the virtual function? Yes, this is the prototype in the design mode: a virtual member function of a clone (). Class meal {public: virtual ~ meal (); Virtual void Eat () = 0; Virtual meal * clone () const = 0; // ...};

Class spaghetti: public meal {public: spaghetti (const spaghetti &); // copy ctor void Eat (); spaghetti * clone () const {return new spaghetti (* this);} // call copy ctor // ... }

Const meal * m = new spaghetti; meal * mymeal = m-> clone ();

Item 30. Factory Method Oh, "Design Mode" book is more detailed.

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

New Post(0)