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

削除された内容 追加された内容
Yak! (トーク | 投稿記録)
現内容を翻訳
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, '''behaves-as-a''') relationship. Sometimes it is useful to call life-cycle management (creation, copy, and destruction) functions of a class hierarchy polymorphically.
それは、'''is-a(~は~である)''' (より現実的には '''behaves-as-a(~として振る舞う)''')関係を実装する方法の一つである。
時々クラス階層中の生存期間管理(生成、コピー、破棄)関数を多態的に呼び出すことが便利な場合がある。
 
C++ は、仮想デストラクタを用いることでオブジェクトの多態的な破棄に(言語組み込みの機能で)対応している。
C++ natively supports polymorphic destruction of objects using a virtual destructor. An equivalent support for creation 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++.
オブジェクトの生成やコピーに対しては同様の対応は存在しない。
C++ では、オブジェクトの生成には常にその型をコンパイル時に知っている必要がある。
仮想コンストラクタ(Virtual Constructor)イディオムは、C++ で多態的なオブジェクトの生成やコピーを可能とする。
 
=== Solution and Sample Code解法とサンプルコード ===
以下のように、仮想コンストラクタの効果は、オブジェクトの生成に create() メンバ関数を用い、コピー生成に clone() メンバ関数を用いることで得られる。
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 () {} // Destructorデストラクタ
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>
 
Manager クラスは 2 つの純粋仮想関数を実装し、型名(Manager)を用いてその型のオブジェクトを生成する。
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.
duplicate 関数は、どのように仮想コンストラクタイディオムが使用されるかを示している。
duplicate 関数は、実際にはなにを複製しているかを知らない。
(実際には Manager かもしれないし Programmer かもしれない) Employee を複製していることを知っているだけである。
正しいインスタンスを生成する責任は派生クラスに委譲されている。
それゆえ、
Employee を頂点とするクラス階層に将来さらに派生クラスが追加されたとしても、duplicate 関数は変更に対して影響を受けない。
 
Manager クラスの clone および create メンバ関数の返値の型は Employee ではなく、そのクラス自身(Manager)である。
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.'''
C++ は、オーバーライドした関数の返値の型を、基本クラス中での関数の返値の型の派生クラスの型にすることができるという自由度を認めている。
この言語機能は、'''共変の返値型(co-variant return types)'''として知られている。
 
リソースの所有権を正しく扱うため、clone() および create() 関数をファクトリ関数とみなし、その返値に対して、[[More C++ Idioms/リソースの返値(Resource Return)|リソースの返値(Resource Return)]]イディオムを用いるべきである。
To handle resource ownership properly, the [[More C++ Idioms/Resource Return|Resource Return]] idiom should be employed for the return types of clone() and create() functions as they are factory functions. If used, the return types (shared_ptr<Employee> and shared_ptr<Manager>) are no longer covariant return types and program should fail to compile. In such a case, the virtual constructor functions in the derived class should return the exact type as in the parent class.
使用した場合、返値の型は(例えば shared_ptr<Employee> と shared_ptr<Manager> のようになり)、もはや共変の返値型ではなくなりコンパイルに失敗するはずである。
そのような場合、派生クラスの仮想コンストラクタ関数は親クラスでの正確な型を返さなければならない。
 
<source lang="cpp">
55 ⟶ 70行目:
public:
typedef std::tr1::shared_ptr<Employee> Ptr;
virtual ~Employee () {} // Native support for polymorphic destruction.多態的破棄の言語組み込み対応
virtual Ptr create () const = 0; // Virtual constructor 仮想コンストラクタ(creation生成)
virtual Ptr clone () const = 0; // Virtual constructor 仮想コンストラクタ(copyingコピー)
};
class Manager : public Employee // "is-a" relationship関係
{
public:
Manager () {} // Default constructorデフォルトコンストラクタ
Manager (Manager const &) {} // Copy constructorコピーコンストラクタ
~Manager () {}
Ptr create () const // Virtual constructor 仮想コンストラクタ(creation生成)
{
return Ptr(new Manager());
}
Ptr clone () const // Virtual constructor 仮想コンストラクタ(copyingコピー)
{
return Ptr(new Manager (*this));
76 ⟶ 91行目:
</source>
 
=== Known Uses既知の利用 ===
 
=== Related Idioms関連するイディオム ===
* [[More C++ Idioms/リソースの返値(Resource Return)|リソースの返値(Resource Return)]]
 
=== References ===
* [http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.8 Virtual Constructor]
 
[[Category:{{BASEPAGENAME}}|{{SUBPAGENAME}}]]
<noinclude>
[[en:More C++ Idioms/Virtual Constructor]]
</noinclude>
[[Category:{{BASEPAGENAME}}|{{SUBPAGENAME}}かそうこんすとらくた]]