To be honest there are a couple of questions I'd like to ask, but will use only one question to do them.
By what I've seen static functions can be accessed externally without the needs of a class object to be created so I assume these functions are from a default copy created at the program initialization. When a class has private constructor for single usage usually and a known method GetInstance is used, the address of a static variable is returned which the pointer will point to. The thing is you can call GetInstance many times but the address where the pointer points is always the same, why this occurs and second, what is the difference about it and direct static functions? I know that on GetInstance I can access storage vector because a "COPY" of is created (refer to the question above) and the function StoreB has a this pointer which also led me to the question, why static functions don't have a this pointer, because no copy is created?
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 }
GetInstanceis used to only have one object made. A class implementing that is typically a singleton.