3

I am sorry if this question has been asked before (I tried to find it but in vain). I was going through Singleton design pattern at http://sourcemaking.com/design_patterns/singleton/cpp/1 (code is copied from there)

class GlobalClass
{
    int m_value;
    static GlobalClass *s_instance;
    GlobalClass(int v = 0)
    {
        m_value = v;
    }
  public:
    int get_value()
    {
        return m_value;
    }
    void set_value(int v)
    {
        m_value = v;
    }
    static GlobalClass *instance()
    {
        if (!s_instance)
          s_instance = new GlobalClass;
        return s_instance;
    }
};

// Allocating and initializing GlobalClass's
// static data member.  The pointer is being
// allocated - not the object inself.
GlobalClass *GlobalClass::s_instance = 0;

void foo(void)
{
  GlobalClass::instance()->set_value(1);
  cout << "foo: global_ptr is " << GlobalClass::instance()->get_value() << '\n';
}

void bar(void)
{
  GlobalClass::instance()->set_value(2);
  cout << "bar: global_ptr is " << GlobalClass::instance()->get_value() << '\n';
}

int main()
{
  cout << "main: global_ptr is " << GlobalClass::instance()->get_value() << '\n';
  foo();
  bar();
}

My question is in the lines if (!s_instance) s_instance = new GlobalClass;

s_instance = new GlobalClass will call the constructor but constructor is non-static and we are calling it from a static function. How does that work? Is Constructor "special" in this regard?

Thanks!

Anon
  • 2,608
  • 6
  • 26
  • 38
  • the only thing you can't do is call a class method from inside a static method of the same class. That's obvious because method needs an object upon which to be called, meanwhile static methods don't. – Heisenbug May 02 '11 at 08:37
  • 3
    Note that Singletons are heavily frowned upon nowadays. Watch [this video](http://www.youtube.com/watch?v=-FRm3VPhseI) to learn why. Summary: Singletons are globals, globals introduce implicit dependencies, and implicit dependencies are a nightmare to test, debug and maintain. – fredoverflow May 02 '11 at 08:37
  • Keep in mind that every program starts in the main function and main is a static function. So yes, you can. – Robert Massaioli May 02 '11 at 08:37
  • Read this: http://stackoverflow.com/questions/1008019/c-singleton-design-pattern/1008289#1008289 – Martin York May 02 '11 at 08:40
  • 1
    If you are going to use a singelton (which you probably should not) Then please do not implement it like that. Pointers are horrible and should not be used if you don't need too (and you don't here (if fact pointers make the singelton harder to do correctly). Ask yourself this question how/when is the singelton going to be destroyed? – Martin York May 02 '11 at 08:43
  • What's the advantage of your class over a simple `int global_value;`? – sbi May 02 '11 at 09:35

4 Answers4

5

You can call a constructor from anywhere. If you couldn't call a constructor without an instance, how would you get any instances in the first place?

You've probably called them from plain functions countless times. Static functions are no different.

Matti Virkkunen
  • 63,558
  • 9
  • 127
  • 159
2

Yes constructor can be called from a static function. Constructor is automatically called by the compiler whenever a new object is created. It facilitates for proper initialization of the class members. Yes In a way you can consider Constructor as a special function which can be called from a static function.

In your code above, You are implementing a design pattern called as Singleton.

Alok Save
  • 202,538
  • 53
  • 430
  • 533
  • Thanks for your quick reply. I would like to read more about it.Where can I find more information? for e.g. Whether it is defined in the standard (if yes, where). Do you have some handy links for me to dive into? Some trustworthy source would do just fine :) Thanks again. – Anon May 02 '11 at 08:55
  • @Anon: It is not a C++ standard. You are just creating a new object inside a static function, which is no way different that creating a object from main() or any other static function. I said it so for sake of simplicity. All you have to remember is an constructor will be called whenever an object is created(irrespective of where the objet is being created) – Alok Save May 02 '11 at 11:05
1

Point 1: In C++, you cannot call a non-static member function inside a static member function. The reason is that there is no "this" pointer in static function. Constructors are no exception.

class A
{
public:
    void fun(){};
    static void static_fun()
    {
        // This is illegal because fun() is non-static
        fun();
    }
};

Point 2: You can always call the methods of a different object, even if it's non-static!

class A
{
public:
    void fun(){};
    static void static_fun(A& a)
    {
        // Legal here because it's a (different) object..
        a.fun();
    }
};

Conclusion

It has nothing to do with constructor here in your code. Because you are creating another object. You can call all its public methods including its constructor.

Eric Z
  • 14,327
  • 7
  • 45
  • 69
0

Yes,

  • trivially: use new ClassName --> it will call the constructor (on a new instance...)
  • non-trivially: placement new (useful only for very explicit, library implementation type, scenarios)
sehe
  • 374,641
  • 47
  • 450
  • 633