良い質問!私はそれについて研究し、実験することで何か新しいことを学びました。
あなたはあなたのコメントに正しいです、::S(); //Is ::S a nested name specifier <-- Yes, Indeed!
名前空間の作成を開始すると、それが理解できるようになります。変数は名前空間全体で同じ名前を持つことができ、::
演算子はそれらを区別するものです。名前空間は、ある意味ではクラスのようなものであり、抽象化のもう 1 つのレイヤーです。名前空間で退屈させたくありません。この例のネストされた名前指定子を理解できないかもしれません...これを考慮してください:
#include <iostream>
using namespace std;
int count(0); // Used for iteration
class outer {
public:
static int count; // counts the number of outer classes
class inner {
public:
static int count; // counts the number of inner classes
};
};
int outer::count(42); // assume there are 42 outer classes
int outer::inner::count(32768); // assume there are 2^15 inner classes
// getting the hang of it?
int main() {
// how do we access these numbers?
//
// using "count = ?" is quite ambiguous since we don't explicitly know which
// count we are referring to.
//
// Nested name specifiers help us out here
cout << ::count << endl; // The iterator value
cout << outer::count << endl; // the number of outer classes instantiated
cout << outer::inner::count << endl; // the number of inner classes instantiated
return 0;
}
::count
単純に を使用できる場所でを使用したことに注意してくださいcount
。::count
グローバル名前空間を参照します。
したがって、あなたの場合、 S() はグローバル名前空間にあるため (つまり、同じファイルまたはインクルード ファイルまたは によってエンベロープされていないコードの一部で宣言されているため、 or ;namespace <name_of_namespace> { }
を使用できます。new struct ::S
new struct S
この質問に答えたいと思っていたので、これを学んだので、より具体的で学んだ答えがあれば、共有してください:)