Vinícius
Vinícius

Reputation: 15718

What is the difference between static functions to a static pointer to the class

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


}

Upvotes: 0

Views: 850

Answers (4)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726499

Static functions have no this pointer because they do not have access to instance variables. That is the reason why you declared them static in the first place (in OO literature static functions of C++ are often referred to as class functions).

The reason the same pointer is returned from GetInstance is that the variable inside the method stores the pointer in the static storage,

static store obj;

meaning that all invocations of the method will get the same value.

You are also wrong about the copying happening when you call instance functions. The pointer this points to the object itself, so the changes that you make to storage are performed on the actual object.

Upvotes: 1

john
john

Reputation: 87959

No function of any sort, static, member or free, is created. They all exist, as part of the program, at all times. They do not pop into existence with the creation of objects. That seems to be your biggest misunderstanding.

Upvotes: 3

Luchian Grigore
Luchian Grigore

Reputation: 258568

You've stumbled across the singleton pattern. A single instance of the class store is allowed through the program, and it's accessible via the GetInstance() method. The object static store obj; is only initialized once and a pointer to it returned every time you call GetInstance().

why static functions don't have a this pointer, because no copy is created?

Because they're not associated with an instance of the class.

The thing is you can call GetInstance many times but the address where the pointer points is always the same, why this occurs?

Already explained, it's the same object.

and second, what is the difference about it and direct static functions?

There's no such thing as direct static function.

Upvotes: 1

Lily Ballard
Lily Ballard

Reputation: 185671

Your assumption is flawed. Static functions don't use some private default copy created at program initialization. They simply use no copy whatsoever. A static function is effectively just a namespaced function that has access to the static members of the class, and any nested types. You are correct when you say there is no this pointer, because there is no instance of the class whatsoever.

In fact, you can implement your static method as a global function without any change in behavior. Just pull it out of the class, mark it as a friend to the class, and then qualify any accesses to static members/nested types by using the class namespace qualifier (e.g. store::).


As for the question about GetInstance(), that always returns the same pointer because it's using a static variable. Static variables are basically globals that are only visible within the local scope. So any access to that same static variable always has the same value.

This is called the singleton pattern.

Upvotes: 3

Related Questions