「More C++ Idioms/名前付きコンストラクタ(Named Constructor)」の版間の差分

削除された内容 追加された内容
M en:More C++ Idioms/Named Constructor の 4版: oldid=1290932
Yak! (トーク | 投稿記録)
現内容を翻訳
1 行
=<center>名前付きコンストラクタ(Named Constructor)</center>=
=== Intent意図 ===
* クラスのオブジェクトを生成する可読性が高く直感的な方法を提供する。
* To have a readable and intuitive way of creating objects of a class
* クラスのオブジェクトを生成する際に特定の制約を強制する。
* To impose certain constraints while creating objects of a class
 
=== Also Known As別名 ===
 
=== Motivation動機 ===
C++ では、コンストラクタはパラメータの型、順序、数によってのみ互いに区別される。もちろん、クラスが複数のコンストラクタを持つ時、それぞれのコンストラクタは異なる目的を持っている。しかし、C++ ではクラスのインタフェースに対して ''意味的な'' 違いを表現することは困難である。なぜなら全てのコンストラクタは同じ名前を持ちパラメータによってのみ区別されうるからである。パラメータの型、順序、数のみが異なる多数のコンストラクタ呼び出しを伴うコードを読むことは、クラスのオリジナルな作者以外には非常に非直感的なものとなる。名前付きコンストラクタ(Named Constructor)イディオムはこの問題に対処する。
In C++, constructors are distinguished from each other only based on the type, the order and the number of parameters. Of course when a class has multiple constructors, each constructor has a different purpose. However, in C++ it is hard to capture that "semantic" difference in the interface of the class because all the constructors have the same name and only parameters can distinguish between them. Reading code with lots of constructor calls only differing in the type/order/number of parameters is quite unintuitive except for the original developer of the class. Named constructor idiom addresses the problem.
 
=== Solution and Sample Code解法とサンプルコード ===
名前付きコンストラクタ(Named Constructor)イディオムは、コンストラクタの代わりにオブジェクトを生成する意味のある名前を持った一組の静的メンバ関数を利用する。コンストラクタは private か protected でクライアントは public な静的関数のみにアクセスできる。その静的関数群は ''名前付きコンストラクタ(named constructor)'' と呼ばれ、オブジェクトを生成するそれぞれ独自の方法と、それぞれ異なった直感的な名前を持つ。以下の例を考えてみよう。
The named constructor idiom uses a set of static member functions with meaningful names to create objects instead of constructors. Constructors are either private or protected and clients have access only to the public static functions. The static functions are called "named constructors" as each unique way of creating an object has a different intuitive name. Consider the example below:
 
<source lang="cpp">
16 行
{
public:
static Game createSinglePlayerGame() { return Game(0); } // named constructor名前付きコンストラクタ
static Game createMultiPlayerGame() { return Game(1); } // named constructor名前付きコンストラクタ
protected:
Game (int game_type);
23 行
int main(void)
{
Game g1 = Game::createSinglePlayerGame(); // Using named constructor名前付きコンストラクタの利用
Game g2 = Game(1); // multiplayer game; without named constructor名前付きコンストラクタなしでの複数プレイヤーゲーム (does not compileコンパイルされない)
}
</source>
 
上記クラスにおいて名前付きコンストラクタ(Named Constructor)イディオムなしの場合、''Game(1)'' と ''Game(0)'' が何を意味するのか把握することは難しい。このイディオムはその意図をはっきり明確にする。さらに、このイディオムによりオブジェクトの生成プロセスに特定の制約を課することができる。例えば、名前付きコンストラクタが常に ''new'' を使って動的にオブジェクトを作成するようにできる。そのような場合、[[More C++ Idioms/リソースの返値(Resource Return)|リソースの返値(Resource Return)]]イディオムも有用となるだろう。
Without using the named constructor idiom in the class above, it is difficult to convey the meaning of what ''Game(1)'' and ''Game(0)'' means. The idiom makes it loud and clear! Additionally, it is possible to put certain constraints on the object creation process using this idiom. For example, named constructors could always create an object dynamically using ''new''. In such a case, [[More C++ Idioms/Resource Return|Resource Return]] idiom could be helpful.
 
=== Known Uses既知の利用 ===
 
=== Related Idioms関連するイディオム ===
[[More C++ Idioms/リソースの返値(Resource Return)|リソースの返値(Resource Return)]]
 
=== References ===
http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.8
<noinclude>
[[Category:{{BASEPAGENAME}}|{{SUBPAGENAME}}]]
[[en:More C++ Idioms/Named Constructor]]
</noinclude>
[[Category:{{BASEPAGENAME}}|{{SUBPAGENAME}}なまえつきこんすとらくた]]