- 名前空間は、変数、メソッド、クラスなどの識別子を定義または宣言できる空間を提供します。
- ネームスペースを使用すると、変数、メソッド、クラスなどの識別子が定義されるスペースまたはコンテキストを定義できます。本質的に、名前空間はスコープを定義します。
名前の衝突を避けるためのネームスペースの利点。
- たとえば、xyz() という関数を持つコードを作成していて、同じ関数 xyz() を持つ別のライブラリが利用可能であるとします。現在、コンパイラは、コード内で参照している xyz() 関数のバージョンを知る方法がありません。
- 名前空間は、この困難を克服するために設計されており、異なるライブラリで使用可能な同じ名前の類似した関数、クラス、変数などを区別するための追加情報として使用されます。
- 名前空間スコープの最良の例は、すべてのクラス、メソッド、およびテンプレートが宣言されている C++ 標準ライブラリ (std) です。したがって、C++ プログラムを作成するときは、通常、名前空間 std を使用してディレクティブを含めます。
名前空間の定義:
- 名前空間定義は、次のようにキーワード namespace で始まり、その後に名前空間名が続きます。
namespace namespace_name { // code declarations i.e. variable (int a;) method (void add();) classes ( class student{};) }> - なお、右中括弧の後にはセミコロン(;)がありません。
- 関数または変数の名前空間対応バージョンを呼び出すには、次のように名前空間名を先頭に追加します。
- 名前空間名: :code; // コードは変数、関数、クラスのいずれかです。
using ディレクティブ:
- using namespace ディレクティブを使用して名前空間の先頭に追加することを避けることもできます。このディレクティブは、後続のコードが指定された名前空間内の名前を使用していることをコンパイラーに伝えます。
- したがって、名前空間は次のコードに暗黙的に含まれます。
C++
#include> using> namespace> std;> // first name space> namespace> first_space> {> >void> func()> >{> >cout <<>'Inside first_space'> << endl;> >}> }> // second name space> namespace> second_space> {> >void> func()> >{> >cout <<>'Inside second_space'> << endl;> >}> }> using> namespace> first_space;> int> main ()> {> >// This calls function from first name space.> >func();> >return> 0;> }> |
>
>出力
Inside first_space>
- using ディレクティブで導入された名前は、通常のスコープ ルールに従います。名前は、using ディレクティブの位置から、そのディレクティブが見つかるスコープの終わりまで表示されます。外側のスコープで定義された同じ名前のエンティティは非表示になります。
ネストされた名前空間:
- 名前空間はネストでき、次のように 1 つの名前空間を別の名前空間内に定義できます。
SYNTAX: namespace namespace_name1 { // code declarations namespace namespace_name2 { // code declarations } }> 次のように解決演算子を使用して、ネストされた名前空間のメンバーにアクセスできます。
// namespace_name2 のメンバーにアクセスする
ネームスペース namespace_name1::namespace_name2 を使用します。
// namespace_name1 のメンバーにアクセスするには
ネームスペース namespace_name1 を使用します。
上記のステートメントで namespace_name1 を使用している場合、次のようにスコープ内で namespace_name2 の要素が使用可能になります。
C++
#include> using> namespace> std;> // first name space> namespace> first_space> {> >void> func()> >{> >cout <<>'Inside first_space'> << endl;> >}> >// second name space> >namespace> second_space> >{> >void> func()> >{> >cout <<>'Inside second_space'> << endl;> >}> >}> }> using> namespace> first_space::second_space;> int> main ()> {> >// This calls function from second name space.> >func();> > >return> 0;> }> |
>
>出力
Inside second_space>
名前空間が変数や関数を含むエンティティをどのようにスコープするかを見てみましょう。
C++
#include> using> namespace> std;> // first name space> namespace> first_space> {> >void> func()> >{> >cout <<>'Inside first_space'> << endl;> >}> }> // second name space> namespace> second_space> {> >void> func()> >{> >cout <<>'Inside second_space'> << endl;> >}> }> int> main ()> {> >// Calls function from first name space.> >first_space :: func();> >// Calls function from second name space.> >second_space :: func();> >return> 0;> }> |
>
>出力
Inside first_space Inside second_space>
次の C++ プログラムを考えてみましょう。
CPP
// A program to demonstrate need of namespace> int> main()> {> >int> value;> >value = 0;> >double> value;>// Error here> >value = 0.0;> }> |
配列Javaに追加
>
>
出力:
Compiler Error: 'value' has a previous declaration as 'int value'>
各スコープでは、名前は 1 つのエンティティのみを表すことができます。したがって、同じスコープ内に同じ名前の 2 つの変数が存在することはできません。名前空間を使用すると、同じ名前を持つ 2 つの変数またはメンバー関数を作成できます。
CPP
// Here we can see that more than one variables> // are being used without reporting any error.> // That is because they are declared in the> // different namespaces and scopes.> #include> using> namespace> std;> // Variable created inside namespace> namespace> first {> int> val = 500;> }> // Global variable> int> val = 100;> int> main()> {> >// Local variable> >int> val = 200;> >// These variables can be accessed from> >// outside the namespace using the scope> >// operator ::> >cout << first::val <<>'
'>;> >return> 0;> }> |
>
>出力
500>
定義と作成: 名前空間を使用すると、名前付きエンティティをグループ化できます。 グローバルスコープ より狭い範囲に分割し、 名前空間のスコープ 。これにより、プログラムの要素を名前で参照されるさまざまな論理スコープに編成することができます。名前空間は、変数、メソッド、クラスなどの識別子を定義または宣言できる空間を提供します。
- ネームスペースは C++ で追加された機能であり、C には存在しません。
- ネームスペースは、その内部の識別子 (関数、変数、またはその他のユーザー定義のデータ型の名前) にスコープを提供する宣言領域です。
- 同じ名前を持つ複数の名前空間ブロックが許可されます。これらのブロック内のすべての宣言は、名前付きスコープで宣言されます。
名前空間定義はキーワードで始まります 名前空間 次のように、名前空間名が続きます。
namespace namespace_name { int x, y; // code declarations where // x and y are declared in // namespace_name's scope }> - 名前空間宣言はグローバル スコープでのみ表示されます。
- 名前空間宣言は、別の名前空間内でネストできます。
- 名前空間宣言にはアクセス指定子 (パブリックまたはプライベート) がありません。
- 名前空間の定義の右中括弧の後にセミコロンを付ける必要はありません。
- 名前空間の定義を複数のユニットに分割できます。
名前空間の定義:
名前空間定義は、次のようにキーワード namespace で始まり、その後に名前空間名が続きます。
C++
namespace> namespace_name{> >// code declarations i.e. variable (int a;)> >method (>void> add();)> >classes (>class> student{};)> }> |
>
>
右中括弧の後にセミコロン (;) がないことに注意してください。
関数または変数の名前空間対応バージョンを呼び出すには、次のように名前空間名を先頭に追加します。
名前空間名: :code; // コードは変数、関数、またはクラスです。
C++
// Let us see how namespace scope the entities including variable and functions:> #include> using> namespace> std;> // first name space> namespace> first_space> {> >void> func()> >{> >cout <<>'Inside first_space'> << endl;> >}> }> // second name space> namespace> second_space> {> >void> func()> >{> >cout <<>'Inside second_space'> << endl;> >}> }> int> main ()> {> >// Calls function from first name space.> >first_space::func();> >// Calls function from second name space.> >second_space::func();> >return> 0;> }> // If we compile and run above code, this would produce the following result:> // Inside first_space> // Inside second_space> |
>
>出力
Inside first_space Inside second_space>
using ディレクティブ:
using namespace ディレクティブを使用すると、名前空間の先頭に追加することを回避できます。このディレクティブは、後続のコードが指定された名前空間内の名前を使用していることをコンパイラーに伝えます。したがって、名前空間は次のコードに暗黙的に含まれます。
C++
#include> using> namespace> std;> // first name space> namespace> first_space> {> >void> func()> >{> >cout <<>'Inside first_space'> << endl;> >}> }> // second name space> namespace> second_space> {> >void> func()> >{> >cout <<>'Inside second_space'> << endl;> >}> }> using> namespace> first_space;> int> main ()> {> >// This calls function from first name space.> >func();> >return> 0;> }> // If we compile and run above code, this would produce the following result:> // Inside first_space> |
>
>出力
Inside first_space>
名前空間全体にアクセスする代わりに、別のオプション ( を使用して 宣言) は、名前空間内の特定の項目にアクセスすることを意味します。たとえば、使用する予定の std 名前空間の唯一の部分が cout である場合は、次のように参照できます。
std::cout を使用します。
後続のコードは、名前空間を先頭に追加しなくても cout を参照できますが、std 名前空間の他の項目は次のように明示的に指定する必要があります。
C++
#include> using> std::cout;> int> main ()> {> >cout <<>'std::endl is used with std!'> << std::endl;> >return> 0;> }> |
>
>出力
std::endl is used with std!>
using ディレクティブで導入された名前は、通常のスコープ ルールに従います。つまり、using ディレクティブが発生した時点から、ディレクティブが見つかったスコープの終わりまで表示されます。外側のスコープで定義された同じ名前のエンティティは非表示になります。
C++
// Creating namespaces> #include> using> namespace> std;> namespace> ns1 {> int> value() {>return> 5; }> }>// namespace ns1> namespace> ns2 {> const> double> x = 100;> double> value() {>return> 2 * x; }> }>// namespace ns2> int> main()> {> >// Access value function within ns1> >cout << ns1::value() <<>'
'>;> >// Access value function within ns2> >cout << ns2::value() <<>'
'>;> >// Access variable x directly> >cout << ns2::x <<>'
'>;> >return> 0;> }> |
>
>
出力:
10/50
5 200 100>
クラスと名前空間: 以下は、名前空間にクラスを作成する簡単な方法です。
C++
// A C++ program to demonstrate use of class> // in a namespace> #include> using> namespace> std;> namespace> ns> {> >// A Class in a namespace> >class> geek> >{> >public>:> >void> display()> >{> >cout<<>'ns::geek::display()'>< } }; } int main() { // Creating Object of geek Class ns::geek obj; obj.display(); return 0; }> |
>
>出力
ns::geek::display()>
クラスは名前空間内で宣言したり、名前空間外で定義したりすることもできます。 次の構文を使用します。
CPP
// A C++ program to demonstrate use of class> // in a namespace> #include> using> namespace> std;> namespace> ns {> // Only declaring class here> class> geek;> }>// namespace ns> // Defining class outside> class> ns::geek {> public>:> >void> display() { cout <<>'ns::geek::display()
'>; }> };> int> main()> {> >// Creating Object of geek Class> >ns::geek obj;> >obj.display();> >return> 0;> }> |
>
>出力
ns::geek::display()>
名前空間の外側でもメソッドを定義できます 。以下はコード例です。
C++
// A C++ code to demonstrate that we can define> // methods outside namespace.> #include> using> namespace> std;> // Creating a namespace> namespace> ns {> void> display();> class> geek {> public>:> >void> display();> };> }>// namespace ns> // Defining methods of namespace> void> ns::geek::display()> {> >cout <<>'ns::geek::display()
'>;> }> void> ns::display() { cout <<>'ns::display()
'>; }> // Driver code> int> main()> {> >ns::geek obj;> >ns::display();> >obj.display();> >return> 0;> }> |
>
>
出力:
ns::display() ns::geek::display():>
ネストされた名前空間:
名前空間は入れ子にすることができます。つまり、次のように 1 つの名前空間を別の名前空間内に定義できます。
C++
ニック・プロス ブラック・ライトニング
namespace> namespace_name1 {> >// code declarations> >namespace> namespace_name2 {> >// code declarations> >}> }> |
>
>
次のように解決演算子 (::) を使用して、ネストされた名前空間のメンバーにアクセスできます。
C++
// to access members of namespace_name2> using> namespace> namespace_name1::namespace_name2;> // to access members of namespace:name1> using> namespace> namespace_name1;> |
>
>
上記のステートメントで、namespace_name1 を使用している場合、次のようにスコープ内で namespace_name2 の要素が使用可能になります。
C++
#include> using> namespace> std;> // first name space> namespace> first_space> {> >void> func()> >{> >cout <<>'Inside first_space'> << endl;> >}> >// second name space> >namespace> second_space> >{> >void> func()> >{> >cout <<>'Inside second_space'> << endl;> >}> >}> }> using> namespace> first_space::second_space;> int> main ()> {> > >// This calls function from second name space.> >func();> > >return> 0;> }> // If we compile and run above code, this would produce the following result:> // Inside second_space> |
>
>出力
Inside second_space>
ネームスペースには、名前の衝突を避けるという利点があります。
たとえば、xyz() という関数を持つコードを作成していて、同じ関数 xyz() を持つ別のライブラリがコード内で利用可能であるとします。現在、コンパイラは、コード内で参照している xyz() 関数のバージョンを知る方法がありません。
名前空間は、この困難を克服するために設計されており、異なるライブラリで使用可能な同じ名前の類似した関数、クラス、変数などを区別するための追加情報として使用されます。
名前空間スコープの最良の例は、すべてのクラス、メソッド、およびテンプレートが宣言されている C++ 標準ライブラリ (std) です。したがって、C++ プログラムを作成する際には、通常、ディレクティブを含めます。
名前空間 std を使用します。
C++ の名前空間 |セット 2 (拡張名前空間と名前なし名前空間) C++ の名前空間 |セット 3 (アクセス、ヘッダーの作成、ネストとエイリアス) C++ では名前空間をネストできますか? 参照 : http://www.cplusplus.com/doc/tutorial/namespaces/