Skip to content

Commit a33510f

Browse files
static_member_variable.cpp
1 parent 15e766c commit a33510f

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

static_member_variable.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <vector>
2+
#include <string>
3+
#include <iostream>
4+
5+
//https://www.learncpp.com/cpp-tutorial/static-member-variables/
6+
7+
using namespace std;
8+
9+
class Visitor {
10+
private:
11+
static int count; // this only "declares" the variable!
12+
13+
public:
14+
Visitor() { ++count; }
15+
static int get() { return count; }
16+
};
17+
18+
int Visitor::count = 10; // actually "defines" the variable
19+
20+
int main(int argc, char** argv) {
21+
Visitor v1;
22+
cout << Visitor::get() << endl; // 11
23+
Visitor v2;
24+
cout << Visitor::get() << endl; // 12
25+
26+
return 0;
27+
}

0 commit comments

Comments
 (0)