以下のセット 1 に名前空間を導入しました。
C++ の名前空間 |セット 1 (導入)
名前空間の定義:
名前空間定義は、次のようにキーワード namespace で始まり、その後に名前空間名が続きます。
名前空間 namespace_name
{
// コード宣言、つまり変数 (int a;)
メソッド (void add();)
クラス (クラスの生徒{};)
}
右中括弧の後にセミコロン (;) がないことに注意してください。
関数または変数の名前空間対応バージョンを呼び出すには、次のように名前空間名を先頭に追加します。
名前空間名: :code; // コードは変数関数またはクラスです。
using ディレクティブ:
gimpの色変更
using namespace ディレクティブを使用して名前空間の先頭に追加することを避けることもできます。このディレクティブは、後続のコードが指定された名前空間内の名前を使用していることをコンパイラーに伝えます。
したがって、名前空間は次のコードに暗黙的に含まれます。
#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; }
using ディレクティブで導入された名前は、通常のスコープ ルールに従います。名前は、using ディレクティブの位置から、そのディレクティブが見つかるスコープの終わりまで表示されます。外側のスコープで定義された同じ名前のエンティティは非表示になります。
ネストされた名前空間:
名前空間はネストでき、次のように 1 つの名前空間を別の名前空間内に定義できます。
SYNTAX: namespace namespace_name1 { // code declarations namespace namespace_name2 { // code declarations } } 次のように解決演算子を使用して、ネストされた名前空間のメンバーにアクセスできます。
// namespace_name2 のメンバーにアクセスする
ネームスペース namespace_name1::namespace_name2 を使用します。
// 名前空間:name1 のメンバーにアクセスします
ネームスペース namespace_name1 を使用します。
上記のステートメントでは、namespace_name1 を使用している場合、次のようにスコープ内で namespace_name2 の要素が使用可能になります。
#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; }
グローバル空間に複数の名前空間を作成することもできます。これは 2 つの方法で実行できます。
-
出力:
-
出力:
- これらは同じプログラム内で直接使用でき、一意の識別子の宣言に使用されます。
- 名前のない名前空間では、名前空間の名前が名前空間の宣言に記載されていません。
- 名前空間の名前はコンパイラによって一意に生成されます。
- 作成した名前のない名前空間には、それを作成したファイル内でのみアクセスできます。
- 名前のない名前空間は、変数の静的宣言の代わりになります。
CPP
// A C++ program to show more than one namespaces // with different names. #include using namespace std; // first name space namespace first { int func() { return 5; } } // second name space namespace second { int func() { return 10; } } int main() { // member function of namespace // accessed using scope resolution operator cout << first::func() <<"n"; cout << second::func() <<"n"; return 0; }
5 10
同じ名前を持つ 2 つの名前空間ブロックを作成することもできます。 2 番目の名前空間ブロックは、実際には最初の名前空間の継続に他なりません。簡単に言うと、両方の名前空間は異なるものではなく、部分的に定義されている実際には同じであると言えます。
CPP
// C++ program to demonstrate namespace extension #include using namespace std; // first name space namespace first { int val1 = 500; } // rest part of the first namespace namespace first { int val2 = 501; } int main() { cout << first::val1 <<"n"; cout << first::val2 <<"n"; return 0; }
500 501
名前のない名前空間
// C++ program to demonstrate working of unnamed // namespaces #include using namespace std; // unnamed namespace declaration namespace { int rel = 300; } int main() { cout << rel << "n"; // prints 300 return 0; }
出力:
300