「More C++ Idioms/仮想コンストラクタ(Virtual Constructor)」の版間の差分

削除された内容 追加された内容
編集の要約なし
1 行
=<center>Virtual Constructor</center>=
=== Intent ===
To create a copy of an object or a new object without knowing its concrete type.
 
=== Also Known As ===
 
=== Motivation ===
Uses of calling member functions in a class hierarchy polymorphically are well known in the object-oriented programming community. It is a way of implementing the "is-a" (or more practically the "behaves-as-a") relationship. Sometimes it is useful to call life-cycle management (creation, copy, and destruction) functions of a class hierarchy polymorphically.
 
C++ natively supports polymorphic destruction of objects using a virtual destructor. An equivalent support for cration and coping of objects is missing. In C++, creation of object(s) always requires its type to be known at compile-time. The Virtual Constructor idiom allows polymorphic creation of and coping of objects in C++.
== Solution and Sample Code ==
 
=== Solution and Sample Code ===
The effect of a virtual constructor by a create() member function for creation and a clone() member function for copy construction as shown below.
<source lang="cpp">
class Employee
{
public:
virtual ~Employee () // Native support for polymorphic destruction.
virtual Employee * create () const = 0; // Virtual constructor (creation)
virtual Employee * clone () const = 0; // Virtual constructor (copying)
};
class Manager : public Employee // "is-a" relationship
{
public:
Manager (); // Default constructor
Manager (Manager const &); // Copy constructor
Manager * create () const // Virtual constructor (creation)
{
return new Manager();
}
Manager * clone () const // Virtual constructor (copying)
{
return new Manager (*this);
}
};
class Programmer : public Employee { /* Very similar to the Manager class */ };
Employee * duplicate (Employee const & e)
{
return e->clone(); // Using virtual constructor idiom.
}
</source>
 
The Manager class implements the two pure virtual functions and uses the type name (Manager) to create them. The function duplicate shows how virtual constructor idiom is used. It does not really know what it is duplicating. It only knows that it is cloning an Employee. The responsibility of creating the right instance is deligated to the derived classes. The duplicate function is therefore closed for modifications even though the class hierarchy rooted at Employee gets more sub-classes added in the future.
== Known Uses ==
 
The return type of the clone and create member functions of the Manager class is not Employee but the class itself. C++ allows this flexibility in types where the return type of the over-ridden function can be a derived type of that of the function in the base class. This language feature is known as ''co-variant return types.''
== Related Idioms ==
 
=== ReferencesKnown Uses ===
 
=== Related Idioms ===
* [More C++ Idioms/Resource Return|Resource Return]
 
=== References ===
http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8
[[Category:{{BASEPAGENAME}}|{{SUBPAGENAME}}]]