Skip to content

Commit aff4125

Browse files
committed
static constructor private
1 parent 15a6977 commit aff4125

3 files changed

Lines changed: 38 additions & 22 deletions

File tree

‎cpp/README.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
1. [Initializer list constructor](initializer_list_constructor.cpp)
4343
1. [Aggregate](aggregate.cpp)
4444
1. [static](static.cpp)
45+
1. [Static constructor for private members](static_constructor_private.cpp)
4546
1. [POD](pod.cpp)
4647
1. Inheritance
4748
1. [Inheritance override](inheritance_override.cpp)

‎cpp/static.cpp‎

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
/*
2-
# static
3-
4-
This is about static inside classes. Outside classes, it is just the same as in C.
5-
6-
In classes, they are just basically globals with a namespace.
7-
*/
1+
// # static
2+
//
3+
// This is about static inside classes. Outside classes, it is just the same as in C.
4+
//
5+
// In classes, they are just basically globals with a namespace.
86

97
#include "common.hpp"
108

@@ -40,15 +38,6 @@ class Class {
4038
// Object data
4139

4240
static OtherClass otherClass;
43-
44-
// https://stackoverflow.com/questions/1197106/static-constructors-in-c-i-need-to-initialize-private-static-objects
45-
static OtherClass otherClassWithJ;
46-
private:
47-
static struct _StaticConstructor {
48-
_StaticConstructor() {
49-
otherClassWithJ.j = otherClassWithJ.i + 1;
50-
}
51-
} _staticConstructor;
5241
public:
5342

5443
// Static methods
@@ -97,10 +86,6 @@ OtherClass Class::otherClass(1);
9786
// ERROR
9887
//Class::otherClass.inc();
9988

100-
OtherClass Class::otherClassWithJ(1);
101-
// Must come after all statics.
102-
Class::_StaticConstructor Class::_staticConstructor;
103-
10489
int main() {
10590
// Basic static field example.
10691
{
@@ -115,8 +100,6 @@ int main() {
115100
c.setIStaticNotStatic(3);
116101
assert(Class::iStatic == 3);
117102
assert(Class::otherClass.i == 1);
118-
assert(Class::otherClassWithJ.i == 1);
119-
assert(Class::otherClassWithJ.j == 2);
120103
//assert(Class::iNoInit == 0);
121104
}
122105

‎cpp/static_constructor_private.cpp‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// https://stackoverflow.com/questions/185844/how-to-initialize-private-static-members-in-c/48337288#48337288
2+
// https://stackoverflow.com/questions/1197106/static-constructors-in-c-i-need-to-initialize-private-static-objects
3+
4+
#include <cassert>
5+
#include <vector>
6+
7+
// Normally on the .hpp file.
8+
class MyClass {
9+
public:
10+
static std::vector<int> v, v2;
11+
static struct _StaticConstructor {
12+
_StaticConstructor() {
13+
v.push_back(1);
14+
v.push_back(2);
15+
v2.push_back(3);
16+
v2.push_back(4);
17+
}
18+
} _staticConstructor;
19+
};
20+
21+
// Normally on the .cpp file.
22+
std::vector<int> MyClass::v;
23+
std::vector<int> MyClass::v2;
24+
// Must come after every static member.
25+
MyClass::_StaticConstructor MyClass::_staticConstructor;
26+
27+
int main() {
28+
assert(MyClass::v[0] == 1);
29+
assert(MyClass::v[1] == 2);
30+
assert(MyClass::v2[0] == 3);
31+
assert(MyClass::v2[1] == 4);
32+
}

0 commit comments

Comments
 (0)