正直に言うと、私が聞きたい質問がいくつかありますが、それらを行うために使用する質問は1つだけです。
私が見たところ、静的関数はクラスオブジェクトを作成しなくても外部からアクセスできるため、これらの関数はプログラムの初期化時に作成されたデフォルトのコピーからのものであると思います。クラスに通常1回使用するためのプライベートコンストラクタがあり、既知のメソッドGetInstance
が使用される場合、ポインタが指す静的変数のアドレスが返されます。問題は、何度も呼び出すことができるということですがGetInstance
、ポインタが指すアドレスは常に同じです。なぜこれが発生するのか、次に、それと直接静的関数の違いは何ですか?の「COPY」が作成され(上記の質問を参照)、関数にポインタがあり、静的関数に静的関数がないのはなぜかという質問につながるため、ベクトルGetInstance
にアクセスできることを知っています。storage
StoreB
this
this
ポインタ、コピーが作成されないため?
class store
{
private:
store(){};
~store(){};
std::vector<int>storage;
public:
static void Store( int a );
void StoreB( int a );
static store* GetInstance()
{
static store obj;
return& obj;
}
};
void store::StoreB( int a )
{
storage.push_back( a );
}
void store::Store( int a )
{
//storage.push_back( a ); //can't
}
int _tmain(int argc, _TCHAR* argv[])
{
store::Store( 2 );
store::GetInstance()->Store( 3 );
store *a = store::GetInstance();
store *b = store::GetInstance();
cout << a << endl //points to the same address
<< b << endl; //points to the same address
}