0

A static variable can be accessed by both static member function as well a regular member function. However, I see that the static member function is often defined like getinstance() to get the static variable Instance. Why is it so? Is there any reason behind it? I can have the regular function that can get the static variable instance also.

BЈовић
  • 62,405
  • 41
  • 173
  • 273
dexterous
  • 6,422
  • 12
  • 51
  • 99
  • 9
    To use a non-static function you need an object instance, and if you have more than one object instance you no longer have a singleton. – Some programmer dude Mar 04 '14 at 09:43
  • +1 just to "remove" the "-1" vote - I don't think this question deserves -1, it can be a bit tricky, especially for beginners and even more for ones, never tried to implement and use singleton. – Kiril Kirov Mar 04 '14 at 09:49
  • @JoachimPileborg - I'd post this as an answer - short and very explanatory and clear. – Kiril Kirov Mar 04 '14 at 09:49

4 Answers4

1

Do you mean "why is it a static member rather than a non-member"? So it's (a) scoped inside the class, and (b) can access the private constructor and instance.

You could make it a non-member, if you really wanted to make life a little harder for everyone involved. You'd need to give it a more verbose name (e.g. get_instance_of_whatever rather than whatever::get_instance), and make it a friend so that it can access, and if necessary create, the instance. It would be more straightforward to make it a static member.

(I assume you don't mean "why is it static rather than non-static". Obviously, you can't call a non-static member without an object, and you can't access an object without calling the accessor function.)

Of course, you shouldn't be implementing a singleton in the first place; especially in C++, where it leads to a minefield of lifetime issues in addition to the general conceptual problems with the anti-pattern.

Community
  • 1
  • 1
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0

To get an object instance you need to call the function, and to call the function you need an object instance. Unless it's a static function.

user207421
  • 305,947
  • 44
  • 307
  • 483
0

Beside holding the instance of the object, the point of the static function instance() in a singleton is that is creates when it is called.

Assuming constructor and destructor are public, you could also create a global variable, or a static variable, but it is created and initialized before entering main(). Take a note that people make them private to prevent creation to have a single instance of the object.

The following example shows what I explained above:

#include <iostream>

struct A
{
    static A& instance()
    {
        static A a;
        return a;
    }

    void foo(){
        std::cout << "A::foo()" << std::endl;
    }

private:
    A(){
        std::cout << "creating A" << std::endl;
    }
    ~A(){
        std::cout << "destroying A" << std::endl;
    }
};

int main() 
{
    std::cout << "1" << std::endl;

    A::instance().foo();

    std::cout << "2" << std::endl;
}
BЈовић
  • 62,405
  • 41
  • 173
  • 273
0

Singleton Design patterns says only one object of that class can be possible.

So to restrict to this condition, we need to make constructor as private so that no object can be created form outside of that class.

now if this is the case then how you will get the object. so there is only one possibility to make a function static so that it can be accessible by using class name and will return an object of that class.

rajenpandit
  • 1,265
  • 1
  • 15
  • 21