My Puzzle: inline static class::method with static variable

example:

1
2
3
4
5
6
7
8
9
10
// singleton_object.h
class SingletonObject
{
public:
    inline static SingletonObject& GetInstance()
    {
        static SingletonObject singleton_object;
        return singleton_object;
    }
};


It's a singleton.
I compile and run it on VC8, it runs OK.

But, I heard my friend he use it before, and there is some problem on it, but he couldn't remember the problem.

Add I test it on static lib and dynamic lib, It runs OK, too.
Indeed, I doubt it, maybe it cann't work in other compiler or other condition that I didn't try. May be some time, the GetInstance() will call the Constructor twice. Or it wasn't inline...

Is there any bug in it?
Could any one help me, Thank you!
Last edited on
As it stands it is too simple - as it uses the compiler provided default constructor, copy constructor, and assignment operator which are public by default - this means that you can create a new one, copy it around and do assignments - whihc all defeats the point of being a 'Singleton'.

You will need to do declarations for these three functions and put them in the private part of the class.
Last edited on
This code should compile with every C++ compiler.

However there is a danger. The name GetInstance does not reflect the implemented definition of the function. In reality, it returns the reference to the same Singleton object which is defined as static inside the function.

The constructor of this static local object will be called only once when the GetInstance function is called for the first time. Run the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <iostream>

using namespace std;

class SingletonObject
{
	public:
	SingletonObject (void)
	{
		cout << "SingletonObject created." << endl;
	}
	
	public:
	inline static SingletonObject& GetInstance (void)
	{
		static SingletonObject singleton_object;
		return singleton_object;
	}
};

int main (void)
{
	cout << "First call." << endl;
	SingletonObject &obj1 = SingletonObject::GetInstance ();

	cout << "Second call." << endl;
	SingletonObject &obj2 = SingletonObject::GetInstance ();

	cout << "Third call." << endl;
	SingletonObject &obj3 = SingletonObject::GetInstance ();

	cout << "Address of obj1: " << &obj1 << endl;
	cout << "Address of obj2: " << &obj2 << endl;
	cout << "Address of obj3: " << &obj3 << endl;
}
krishnendu wrote:
However there is a danger. The name GetInstance does not reflect the implemented definition of the function.


Trivial
Thanks for the suggestions.

I have do some modify, I think it will be better.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
// singleton_object.h
class SingletonObject
{
public:
    // Get an instance of a singleton class
    inline static SingletonObject& GetSingleInstance()
    {
        static SingletonObject singleton_object;
        return singleton_object;
    }

private:
    // cann't not created out of the class
    SingletonObject()
    {
        cout << "SingletonObject is created" << endl;
    }

    // without implementation, non copyable.
    SingletonObject(const SingletonObject& obj);

    // without implementation, non copyable.
    SingletonObject& operator=(const SingletonObject& obj);
};


I want the GetSingleInstance() more effective, so I used the keyword inline,

But is "inline" work?

Maybe GetSingleInstance() is not an inline function...

That's what I worry about now...

Because I want to used it frequently in the game.
Last edited on
lrhnfs wrote:
But is "inline" work?


inline says that the function can be resolved anytime it's used. But then you have a SingletonObject each time you call the function. Which is legal but not what you want. But on the other side a function with inline is not necessarily inline

static says that the function is independend of the surrounding class.

So I'd say that inline may be a problem. Check it out
Thank you, coder777

I think you are right, I know how to solve the problem now.
Topic archived. No new replies allowed.