is there something wrong with my c++ singleton design pattern code? [duplicate] - c++

According to Static data members on the IBM C++ knowledge center:
The declaration of a static data member in the member list of a class is not a definition. You must define the static member outside of the class declaration, in namespace scope.
Why is that? What's the schematic behind that regarding the memory allocation?

It's a rule of the language, known as the One Definition Rule. Within a program, each static object (if it's used) must be defined once, and only once.
Class definitions typically go in header files, included in multiple translation units (i.e. from multiple source files). If the static object's declaration in the header were a definition, then you'd end up with multiple definitions, one in each unit that includes the header, which would break the rule. So instead, it's not a definition, and you must provide exactly one definition somewhere else.
In principle, the language could do what it does with inline functions, allowing multiple definitions to be consolidated into a single one. But it doesn't, so we're stuck with this rule.

It's not about the memory allocation piece at all. It's about having a single point of definition in a linked compilation unit. #Nick pointed this out as well.
From Bjarne's webite https://www.stroustrup.com/bs_faq2.html#in-class
A class is typically declared in a header file and a header file is
typically included into many translation units. However, to avoid
complicated linker rules, C++ requires that every object has a unique
definition. That rule would be broken if C++ allowed in-class
definition of entities that needed to be stored in memory as objects.

As of C++17 you can now define static data members inside a class. See cppreference:
A static data member may be declared inline. An inline static data
member can be defined in the class definition and may specify an
initializer. It does not need an out-of-class definition:
struct X {
inline static int n = 1;
};

According to the definition of static data members, we can define static variables only for once (i.e in class only) and it is shared by every instance of the class. Also, static members can be accessed without any object.
As per OOPS guidelines compiler does not allocate memory to class instead of that it allocates memory to objects, but static members are independent of object, so to allocate memory to static variables, we define the static data members outside of the class, that's why once this variable is declared, it exists till the program executes. Generally static member functions are used to modify the static variables.

Related

Singleton *Singleton::obj = 0; What does that line do in Singleton pattern implementation? [duplicate]

According to Static data members on the IBM C++ knowledge center:
The declaration of a static data member in the member list of a class is not a definition. You must define the static member outside of the class declaration, in namespace scope.
Why is that? What's the schematic behind that regarding the memory allocation?
It's a rule of the language, known as the One Definition Rule. Within a program, each static object (if it's used) must be defined once, and only once.
Class definitions typically go in header files, included in multiple translation units (i.e. from multiple source files). If the static object's declaration in the header were a definition, then you'd end up with multiple definitions, one in each unit that includes the header, which would break the rule. So instead, it's not a definition, and you must provide exactly one definition somewhere else.
In principle, the language could do what it does with inline functions, allowing multiple definitions to be consolidated into a single one. But it doesn't, so we're stuck with this rule.
It's not about the memory allocation piece at all. It's about having a single point of definition in a linked compilation unit. #Nick pointed this out as well.
From Bjarne's webite https://www.stroustrup.com/bs_faq2.html#in-class
A class is typically declared in a header file and a header file is
typically included into many translation units. However, to avoid
complicated linker rules, C++ requires that every object has a unique
definition. That rule would be broken if C++ allowed in-class
definition of entities that needed to be stored in memory as objects.
As of C++17 you can now define static data members inside a class. See cppreference:
A static data member may be declared inline. An inline static data
member can be defined in the class definition and may specify an
initializer. It does not need an out-of-class definition:
struct X {
inline static int n = 1;
};
According to the definition of static data members, we can define static variables only for once (i.e in class only) and it is shared by every instance of the class. Also, static members can be accessed without any object.
As per OOPS guidelines compiler does not allocate memory to class instead of that it allocates memory to objects, but static members are independent of object, so to allocate memory to static variables, we define the static data members outside of the class, that's why once this variable is declared, it exists till the program executes. Generally static member functions are used to modify the static variables.

Static variables linker error: Undefined symbols for architecture x86_64 [duplicate]

According to Static data members on the IBM C++ knowledge center:
The declaration of a static data member in the member list of a class is not a definition. You must define the static member outside of the class declaration, in namespace scope.
Why is that? What's the schematic behind that regarding the memory allocation?
It's a rule of the language, known as the One Definition Rule. Within a program, each static object (if it's used) must be defined once, and only once.
Class definitions typically go in header files, included in multiple translation units (i.e. from multiple source files). If the static object's declaration in the header were a definition, then you'd end up with multiple definitions, one in each unit that includes the header, which would break the rule. So instead, it's not a definition, and you must provide exactly one definition somewhere else.
In principle, the language could do what it does with inline functions, allowing multiple definitions to be consolidated into a single one. But it doesn't, so we're stuck with this rule.
It's not about the memory allocation piece at all. It's about having a single point of definition in a linked compilation unit. #Nick pointed this out as well.
From Bjarne's webite https://www.stroustrup.com/bs_faq2.html#in-class
A class is typically declared in a header file and a header file is
typically included into many translation units. However, to avoid
complicated linker rules, C++ requires that every object has a unique
definition. That rule would be broken if C++ allowed in-class
definition of entities that needed to be stored in memory as objects.
As of C++17 you can now define static data members inside a class. See cppreference:
A static data member may be declared inline. An inline static data
member can be defined in the class definition and may specify an
initializer. It does not need an out-of-class definition:
struct X {
inline static int n = 1;
};
According to the definition of static data members, we can define static variables only for once (i.e in class only) and it is shared by every instance of the class. Also, static members can be accessed without any object.
As per OOPS guidelines compiler does not allocate memory to class instead of that it allocates memory to objects, but static members are independent of object, so to allocate memory to static variables, we define the static data members outside of the class, that's why once this variable is declared, it exists till the program executes. Generally static member functions are used to modify the static variables.

Why can't static members be declared in a block? [duplicate]

According to Static data members on the IBM C++ knowledge center:
The declaration of a static data member in the member list of a class is not a definition. You must define the static member outside of the class declaration, in namespace scope.
Why is that? What's the schematic behind that regarding the memory allocation?
It's a rule of the language, known as the One Definition Rule. Within a program, each static object (if it's used) must be defined once, and only once.
Class definitions typically go in header files, included in multiple translation units (i.e. from multiple source files). If the static object's declaration in the header were a definition, then you'd end up with multiple definitions, one in each unit that includes the header, which would break the rule. So instead, it's not a definition, and you must provide exactly one definition somewhere else.
In principle, the language could do what it does with inline functions, allowing multiple definitions to be consolidated into a single one. But it doesn't, so we're stuck with this rule.
It's not about the memory allocation piece at all. It's about having a single point of definition in a linked compilation unit. #Nick pointed this out as well.
From Bjarne's webite https://www.stroustrup.com/bs_faq2.html#in-class
A class is typically declared in a header file and a header file is
typically included into many translation units. However, to avoid
complicated linker rules, C++ requires that every object has a unique
definition. That rule would be broken if C++ allowed in-class
definition of entities that needed to be stored in memory as objects.
As of C++17 you can now define static data members inside a class. See cppreference:
A static data member may be declared inline. An inline static data
member can be defined in the class definition and may specify an
initializer. It does not need an out-of-class definition:
struct X {
inline static int n = 1;
};
According to the definition of static data members, we can define static variables only for once (i.e in class only) and it is shared by every instance of the class. Also, static members can be accessed without any object.
As per OOPS guidelines compiler does not allocate memory to class instead of that it allocates memory to objects, but static members are independent of object, so to allocate memory to static variables, we define the static data members outside of the class, that's why once this variable is declared, it exists till the program executes. Generally static member functions are used to modify the static variables.

C++: Static Members can't be Defined at Declaration, but Static Function Variables can?

Here are two variables declared with the keyword static:
void fcn() {
static int x = 2;
}
class cls() {
static int y;
};
We all know that in order for cls to link properly, int cls::y needs to be explicitly defined by the programmer exactly once.
Based on the answers to static variables in an inlined function , it seems that even though no out-of-class definition is required for fcn::x , it is guaranteed that even inlined versions of fcn from different compilation units will reference the same fcn::x. If this is true, then the linker has to be smart enough to reach between compilation units and connect multiple instances of "the same" variable to ensure that static function variables perform as expected.
If this is possible for static function variables, it seems to me that it should also be possible for static class members... so why does the standard require a single out-of-class definition of static class members?
Yes, the linker will indeed have to merge different instances of fcn::x. In other words, even though formally the language says that fcn::x has no linkage, physically it will have to be exposed as an external symbol in all object files that contain it. This is how it is typically implemented in practice: your compiler will expose fcx::x as some sort of heavily mangled external name ##$%^&_fcx_x or such (to ensure it can never clash with "real" external names). This is what the linker will use to merge all instances of fcn::x into one.
As for class members... Firstly, it is not really about what is "possible". It is about the language-level concepts of declarations and definitions. It is about One Definition Rule, which is a higher-level concept than what is "possible" based on raw linker features. According to that rule, objects with external linkage shall be defined by the user and shall have one and only one definition. Static data members of the class are objects with external linkage. The rest follows.
Secondly, and more practically, there's another serious issue with static data members. It is their order of initialization. Static data members are [guaranteed to be] initialized no later than when the first function from the containing translation unit is called (which refers to the translation unit that contains the data member definition). And static objects declared in a single translation unit are initialized the order of their definition, top-to bottom. This is an important property of static data member initialization process. Allowing static data members to be defined "automatically" would defy this part of the specification and would require massive changes to this part of the language.
In other words, when you provide a dedicated definition for a static data member of the class, you are not just doing it for ODR compliance, you are actually expressing your desired initialization order for that object.
Meanwhile, static variables inside functions are objects with no linkage. Hence they receive a different treatment at the conceptual level. And they have well-defined order-of-initialization semantics that is completely unaffected by the need to merge multiple definitions into one.

Why does a static data member need to be defined outside of the class?

According to Static data members on the IBM C++ knowledge center:
The declaration of a static data member in the member list of a class is not a definition. You must define the static member outside of the class declaration, in namespace scope.
Why is that? What's the schematic behind that regarding the memory allocation?
It's a rule of the language, known as the One Definition Rule. Within a program, each static object (if it's used) must be defined once, and only once.
Class definitions typically go in header files, included in multiple translation units (i.e. from multiple source files). If the static object's declaration in the header were a definition, then you'd end up with multiple definitions, one in each unit that includes the header, which would break the rule. So instead, it's not a definition, and you must provide exactly one definition somewhere else.
In principle, the language could do what it does with inline functions, allowing multiple definitions to be consolidated into a single one. But it doesn't, so we're stuck with this rule.
It's not about the memory allocation piece at all. It's about having a single point of definition in a linked compilation unit. #Nick pointed this out as well.
From Bjarne's webite https://www.stroustrup.com/bs_faq2.html#in-class
A class is typically declared in a header file and a header file is
typically included into many translation units. However, to avoid
complicated linker rules, C++ requires that every object has a unique
definition. That rule would be broken if C++ allowed in-class
definition of entities that needed to be stored in memory as objects.
As of C++17 you can now define static data members inside a class. See cppreference:
A static data member may be declared inline. An inline static data
member can be defined in the class definition and may specify an
initializer. It does not need an out-of-class definition:
struct X {
inline static int n = 1;
};
According to the definition of static data members, we can define static variables only for once (i.e in class only) and it is shared by every instance of the class. Also, static members can be accessed without any object.
As per OOPS guidelines compiler does not allocate memory to class instead of that it allocates memory to objects, but static members are independent of object, so to allocate memory to static variables, we define the static data members outside of the class, that's why once this variable is declared, it exists till the program executes. Generally static member functions are used to modify the static variables.