3

Code 1:

#include<iostream> class Singleton { private: static Singleton instance; //declaration of static variable public: static Singleton &GetInstance() { return instance; } void Hello() { std::cout << "Hello!"; } }; Singleton Singleton::instance; //definition of static variable int main() { Singleton::GetInstance().Hello(); } 

Code 2:

#include <iostream> class Singleton { public: static Singleton &GetInstance() { static Singleton instance; //declaration of static variable return instance; } void Hello() { std::cout << "Hello!"; } }; int main() { Singleton::GetInstance().Hello(); } 

In Code 1 we're required to define the static variable, but in Code 2, we just declared the static variable in the function Singleton::GetInstance&() and then returned it. Do the declaration and definition happen in the same line in Code 2? and why?

1
  • 2
    Because, within a class definition, static Singleton singleton is a declaration of something that is visible everywhere class is defined (e.g. in every source file that includes the header that defines the class) and must be defined exactly ones. Whereas, the same line in a function definition is a definition, that only exists within scope of that function (and that function can only be defined once). Commented Jul 20, 2021 at 7:35

3 Answers 3

5

The static member variable instance declared inside class Singleton in Code 1 is a class variable.

This means that regardless of the state of different instances of class Singleton, the state of static variable will remain the same for all objects/instances of that particular class. Hence, it needs to be defined outside the class. (Just like a global variable which is available to function of all scopes.). A single copy of static member variable is allocated in memory even if you don't create any instance. Their definition persists through the entire file.

In Code 2, the static variable instance is not a class variable but is simply a local variable inside a static member function. Hence there is no need for it to be defined outside since its scope is limited only to static Singleton &GetInstance() function.

Sign up to request clarification or add additional context in comments.

Comments

2

For classes static variables are stored well... staticly, so they are accessible between each instance of the class. As in, the same variable is accessible between classes. See https://en.wikipedia.org/wiki/Static_variable for what they are. In functions it wont make much sense to define them static as the function has no access outside of it.

In classes you do not need to define them as static. But in this case using a Singleton it is nice as it ensures everything in the program is working with one instance, as per Singleton design. (Does not guarantee thread safety)

Also static functions simply do not need a constructor to run, so they work as simple functions. They do not guarantee that you work with the same data.

Comments

1

[edit: this answer predates the C++14 tag on the question; it assumes C++17 or C++20]

The assumption is wrong:

class Singleton { private: static inline Singleton instance; //definition of static variable // ^^^^^^ 

You just need to give the compiler a warning.

7 Comments

@prehistoricpenguin: The code in the question is correct, but the assumption that you need a separate definition outside the class is wrong. Therefore, the comparison with function-statics breaks down.
It depends on the compiler you are using, this works in C++17.
I am using c++14, and adding inline is giving an error.
@foragerDev: The assumption for questions tagged "C++" is that it applies to the current C++ version, currently C++20. We have special tags for people using older versions, such as C++-11, C++-14 or C++-17
@foragerDev: You're free to your opinion, but the StackOverflow consensus is otherwise. See the C++ tag info, which literally states "Unless the question explicitly mentions which version of the C++ standard is used, it is assumed that the current version is used.".
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.