The return type of the clone and create member functions of the Manager class is not Employee but the class itself. The duplicate function is therefore closed for modifications even though the class hierarchy rooted at Employee gets more sub-classes added in the future. The responsibility of creating the right instance is delegated to the derived classes. It only knows that it is cloning an Employee. It does not really know what it is duplicating. The function duplicate shows how virtual constructor idiom is used. The Manager class implements the two pure virtual functions and uses the type name (Manager) to create them. The effect of a virtual constructor by a create() member function for creation and a clone() member function for copy construction as shown below.Ĭlass Employee The Virtual Constructor idiom allows polymorphic creation of and copying of objects in C++. In C++, creation of object(s) always requires its type to be known at compile-time. An equivalent support for creation and copying of objects is missing. Sometimes it is useful to call life-cycle management (creation, copy, and destruction) functions of a class hierarchy polymorphically.Ĭ++ natively supports polymorphic destruction of objects using a virtual destructor. It is a way of implementing the is-a (or more practically, behaves-as-a) relationship. Uses of calling member functions in a class hierarchy polymorphically are well known in the object-oriented programming community. To create a copy of an object or a new object without knowing its concrete type.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |