How To Initialize Private Static Members in C++ - Stack Overflow
How To Initialize Private Static Members in C++ - Stack Overflow
How To Initialize Private Static Members in C++ - Stack Overflow
- Stack Overflow
569
What is the best way to initialize a private, static data member in C++? I tried this in my
header file, but it gives me weird linker errors:
class foo
private:
static int i;
};
int foo::i = 0;
I'm guessing this is because I can't initialize a private member from outside the class. So
what's the best way to do this?
Jason Baker
176k125125 gold badges355355 silver badges502502 bronze badges
The answers below do not apply for a template class. They say: the initialization must
go into the source file. For a template class, this is neither possible, nor necessary.
– Joachim W
Jun 9 '16 at 15:20
https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c/185848 1/18
2021/10/16 下午11:16 initialization - How to initialize private static members in C++? - Stack Overflow
C++17 allows inline initialization of static data members (even for non-integer types):
inline static int x[] = {1, 2, 3}; . See
en.cppreference.com/w/cpp/language/static#Static_data_members
– Vladimir Reshetnikov
Feb 14 '18 at 21:59
Add a comment
18 Answers
Active
Oldest
Votes
613
The class declaration should be in the header file (Or in the source file if not shared).
File: foo.h
class foo
private:
static int i;
};
int foo::i = 0;
If the initialization is in the header file then each file that includes the header file will have a
definition of the static member. Thus during the link phase you will get linker errors as the
code to initialize the variable will be defined in multiple source files.
The initialisation of the
static int i must be done outside of any function.
Note: Matt Curtis: points out that C++ allows the simplification of the above if the static
member variable is of const int type (e.g. int , bool , char ). You can then declare and
initialize the member variable directly inside the class declaration in the header file:
class foo
private:
};
https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c/185848 2/18
2021/10/16 下午11:16 initialization - How to initialize private static members in C++? - Stack Overflow
Mark
6921212 silver badges2525 bronze badges
Martin York
240k7777 gold badges314314 silver badges536536 bronze badges
Yes. But I am assuming the question has been simplified. Technically the declaration
and definition can all be in a single source file. But that then limits the use of class by
other classes.
– Martin York
Oct 9 '08 at 3:40
11
Note that this isn't just a question of how the value is initialized: const integral types
defined like this may be turned into compile time constants by the implementation.
This isn't always what you want, since it ups the binary dependency: client code needs
recompilation if the value changes.
– Steve Jessop
Oct 9 '08 at 11:19
5
You might add a clarification that int foo::i =0; should not be inside a function
(including the main function). I had it at the beginning of my main function and it
doesn't like that.
– qwerty9967
Apr 15 '13 at 19:33
100
For a variable:
https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c/185848 3/18
2021/10/16 下午11:16 initialization - How to initialize private static members in C++? - Stack Overflow
foo.h:
class foo
private:
static int i;
};
foo.cpp:
int foo::i = 0;
This is because there can only be one instance of foo::i in your program. It's sort of the
equivalent of extern int i in a header file and int i in a source file.
For a constant you can put the value straight in the class declaration:
class foo
private:
static int i;
};
Matt Curtis
22k88 gold badges5858 silver badges6363 bronze badges
Since when, C++ allows to be just good with declaration in-class and no definition for
integral types. Since C++98 itself or C++03 or when ? Please share authentic links
please. C++ standard wording is not in sync with the compilers. They mention the
member shall still be defined if they are used. So, I don't need the C++ Standard
quoting though
– smRaj
Sep 20 '14 at 15:42
2
Since C++17, static members may be defined in the header with the inline keyword.
http://en.cppreference.com/w/cpp/language/static
https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c/185848 4/18
2021/10/16 下午11:16 initialization - How to initialize private static members in C++? - Stack Overflow
"A static data member may be declared inline. An inline static data member can be defined in
the class definition and may specify a default member initializer. It does not need an out-of-
class definition:"
struct X
};
Xunie
38222 silver badges1616 bronze badges
Die in Sente
8,99522 gold badges3333 silver badges3838 bronze badges
33
For future viewers of this question, I want to point out that you should avoid what
monkey0506 is suggesting.
Header files get compiled once for every .cpp file that directly or indirectly #includes
them, and code outside of any function is run at program initialization, before main() .
By putting: foo::i = VALUE; into the header, foo:i will be assigned the value VALUE
(whatever that is) for every .cpp file, and these assignments will happen in an
indeterminate order (determined by the linker) before main() is run.
What if we #define VALUE to be a different number in one of our .cpp files? It will
compile fine and we will have no way of knowing which one wins until we run the program.
Never put executed code into a header for the same reason that you never #include a
.cpp file.
include guards (which I agree you should always use) protect you from something different:
the same header being indirectly #include d multiple times while compiling a single .cpp
file
https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c/185848 5/18
2021/10/16 下午11:16 initialization - How to initialize private static members in C++? - Stack Overflow
CommunityBot
111 silver badge
Joshua Clayton
1,5671616 silver badges2525 bronze badges
You're right about this of course, except in the case of a class template (which isn't
asked about, but I happen to be dealing with a lot). So if the class is fully defined and
not a class template, then put these static members in a separate CPP file, but for class
templates the definition has to be in the same translation unit (e.g., the header file).
– monkey0506
Jan 18 '13 at 20:22
Your argument is really huge stretch. First you cannot #define VALUE because macros
name has ot be a valid identifier. And Even if you could - who would do that? Header
files are for declaration - ? C'mon.. The only cases where you should avoid putting
values in the header is to fight odr-used. And putting the value in the header may lead
to unnecessary recompilation whenever you need to change the value.
– Aleksander Fular
Mar 4 '16 at 15:39
Add a comment
23
With a Microsoft compiler[1], static variables that are not int -like can also be defined in a
header file, but outside of the class declaration, using the Microsoft specific
__declspec(selectany) .
https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c/185848 6/18
2021/10/16 下午11:16 initialization - How to initialize private static members in C++? - Stack Overflow
class A
static B b;
__declspec(selectany) A::b;
Note that I'm not saying this is good, I just say it can be done.
[1] These days, more compilers than MSC support __declspec(selectany) - at least gcc and
clang. Maybe even more.
Johann Gerell
23.8k99 gold badges6666 silver badges119119 bronze badges
Add a comment
18
int foo::i = 0;
Is the correct syntax for initializing the variable, but it must go in the source file (.cpp) rather
than in the header.
Because it is a static variable the compiler needs to create only one copy of it. You have to
have a line "int foo:i" some where in your code to tell the compiler where to put it otherwise
you get a link error. If that is in a header you will get a copy in every file that includes the
header, so get multiply defined symbol errors from the linker.
David Dibben
17.2k66 gold badges4040 silver badges4141 bronze badges
Add a comment
14
If you want to initialize some compound type (f.e. string) you can do something like that:
https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c/185848 7/18
2021/10/16 下午11:16 initialization - How to initialize private static members in C++? - Stack Overflow
class SomeClass {
public:
struct Initializer {
Initializer() {
_list.push_back("FIRST");
_list.push_back("SECOND");
....
return _list;
};
Of course you have to access _list object always by calling getList() method.
Ziezi
6,05333 gold badges3434 silver badges4646 bronze badges
Kris Kwiatkowski
32122 silver badges88 bronze badges
Here is a version of this idiom that does not require creating one method per member
object: stackoverflow.com/a/48337288/895245
– Ciro Santilli 新疆再教育营六四事件法轮功郝海东
Jan 19 '18 at 8:56
Add a comment
11
https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c/185848 8/18
2021/10/16 下午11:16 initialization - How to initialize private static members in C++? - Stack Overflow
I don't have enough rep here to add this as a comment, but IMO it's good style to write your
headers with #include guards anyway, which as noted by Paranaix a few hours ago would
prevent a multiple-definition error. Unless you're already using a separate CPP file, it's not
necessary to use one just to initialize static non-integral members.
#ifndef FOO_H
#define FOO_H
#include "bar.h"
class foo
private:
static bar i;
};
#endif
I see no need to use a separate CPP file for this. Sure, you can, but there's no technical reason
why you should have to.
monkey0506
2,25611 gold badge1919 silver badges2626 bronze badges
24
regarding good style:you should add comment on the closing endif: #endif // FOO_H
– Riga
Jul 4 '12 at 13:08
12
This only works if you have only one compile unit that includes foo.h. If two or more
cpps include foo.h, which is a typical situation, each cpp would declare the same static
variable so the linker would complain with multiple definition of `foo::i' unless you use
a package compilation with the files (compile only one file that include all cpps). But
although package compilation is great the solution to the problem is to declare (int
foo::i = 0;) in a cpp!
– Alejadro Xalabarder
Dec 31 '13 at 2:51
1
https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c/185848 9/18
2021/10/16 下午11:16 initialization - How to initialize private static members in C++? - Stack Overflow
Add a comment
11
main.cpp
#include <cassert>
#include <vector>
class MyClass {
public:
StaticConstructor() {
v.push_back(1);
v.push_back(2);
v2.push_back(3);
v2.push_back(4);
} _staticConstructor;
};
std::vector<int> MyClass::v;
std::vector<int> MyClass::v2;
MyClass::StaticConstructor MyClass::_staticConstructor;
int main() {
assert(MyClass::v[0] == 1);
assert(MyClass::v[1] == 2);
assert(MyClass::v2[0] == 3);
assert(MyClass::v2[1] == 4);
GitHub upstream.
./main.out
See also: static constructors in C++? I need to initialize private static objects
https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c/185848 10/18
2021/10/16 下午11:16 initialization - How to initialize private static members in C++? - Stack Overflow
Add a comment
5
You can also include the assignment in the header file if you use header guards. I have used
this technique for a C++ library I have created. Another way to achieve the same result is to
use static methods. For example...
class Foo
public:
return *MyStatic();
private:
return &mStatic;
The above code has the "bonus" of not requiring a CPP/source file. Again, a method I use for
my C++ libraries.
user2225284
Add a comment
4
https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c/185848 11/18
2021/10/16 下午11:16 initialization - How to initialize private static members in C++? - Stack Overflow
I follow the idea from Karl. I like it and now I use it as well. I've changed a little bit the
notation and add some functionality
#include <stdio.h>
class Foo
public:
static bool isMyStatic (int & num) { return & num == & MyStatic(); }
private:
return mStatic;
};
Foo obj;
obj.GetMyStaticVar () = 3;
this outputs
mystatic value 7
mystatic value 3
is my static 1 0
Alejadro Xalabarder
1,3031717 silver badges1313 bronze badges
Add a comment
4
https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c/185848 12/18
2021/10/16 下午11:16 initialization - How to initialize private static members in C++? - Stack Overflow
This is a common problem for those who starts with C++. Static class member must be
initialized in single translation unit i.e. in single source file.
Unfortunately, the static class member must be initialized outside of the class body. This
complicates writing header-only code, and, therefore, I am using quite different approach.
You can provide your static object through static or non-static class function for example:
class Foo
return object;
void func() {
object += 5;
};
no one special
1,35977 silver badges2323 bronze badges
I'm still a complete n00b as far as C++ goes, but this looks brilliant to me, thank you so
much! I get perfect life-cycle management of the singleton object for free.
– Rafael Kitover
Nov 6 '19 at 15:18
Add a comment
3
https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c/185848 13/18
2021/10/16 下午11:16 initialization - How to initialize private static members in C++? - Stack Overflow
#include <iostream>
class A
private:
static int v;
};
int main()
A a;
return 0;
andrew
2,83733 gold badges2323 silver badges2727 bronze badges
Add a comment
3
class foo
public:
private:
static int i;
};
void foo::set_default(int x) {
i = x;
We would only have to use the set_default(int x) method and our static variable
would be initialized.
https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c/185848 14/18
2021/10/16 下午11:16 initialization - How to initialize private static members in C++? - Stack Overflow
This would not be in disagreement with the rest of the comments, actually it follows the same
principle of initializing the variable in a global scope, but by using this method we make it
explicit (and easy to see-understand) instead of having the definition of the variable hanging
there.
Ziezi
6,05333 gold badges3434 silver badges4646 bronze badges
Add a comment
2
class foo
private:
};
This way doesn't require providing a definition, and avoids making the constant lvalue,
which can save you some headaches, e.g. when you accidentally ODR-use it.
anatolyg
23.8k88 gold badges5151 silver badges116116 bronze badges
Add a comment
1
I just wanted to mention something a little strange to me when I first encountered this.
https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c/185848 15/18
2021/10/16 下午11:16 initialization - How to initialize private static members in C++? - Stack Overflow
in the .h or .hpp, it looks something like this to initialize a static data member of a template
class:
template<typename T>
Tyler Heers
11444 bronze badges
Add a comment
1
Here are all possibilities and errors in one simple example ...
#ifndef Foo_h
#define Foo_h
class Foo
//static int x = 42; // ISO C++ forbids in-class initialization of non-const static member
'Foo::x'
//static int y {7}; // ISO C++ forbids in-class initialization of non-const static member
'Foo::x'
static int x;
static int y;
int m = 42;
int n {7};
};
int Foo::x = 42; // OK in Foo.h if included in only one *.cpp -> *.o file!
// ONLY if the compiler can see both declarations at the same time it,
#endif // Foo_h
But better place this in Foo.cpp. This way you can separately compile each file and link them
later, otherwise Foo:x will be present in multiple object files and cause a linker error. ...
https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c/185848 16/18
2021/10/16 下午11:16 initialization - How to initialize private static members in C++? - Stack Overflow
int Foo::x = 42; // OK in Foo.h if included in only one *.cpp -> *.o file!
cat
2,76111 gold badge2121 silver badges2828 bronze badges
Add a comment
0
//header file
struct MyStruct {
public:
{ "a", 1 },
{ "b", 2 },
...
{ "z", 26 }
};
{ 1, "a" },
{ 2, "b" },
...
{ 26, "z" }
};
return instance;
private:
MyStruct() {};
};
int main(){
std::cout<<MyStruct::Singleton().some_string<<std::endl;
std::cout<<MyStruct::Singleton().some_int<<std::endl;
return 0;
https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c/185848 17/18
2021/10/16 下午11:16 initialization - How to initialize private static members in C++? - Stack Overflow
David Nogueira
35211 silver badge1414 bronze badges
Add a comment
Highly active question. Earn 10 reputation (not counting the association bonus) in order
to answer this question. The reputation requirement helps protect this question from spam
and non-answer activity.
Not the answer you're looking for? Browse other questions tagged
c++ initialization static-members or ask your own question.
https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c/185848 18/18