Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 61b3469

Browse files
static member data and static member functions
1 parent ca7c902 commit 61b3469

File tree

1 file changed

+38
-0
lines changed
  • Learn_CPP_Programming_Deep_Dive/Section 16 Friend and static members/Static_members

1 file changed

+38
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
class Test
6+
{
7+
private:
8+
int a;
9+
int b;
10+
public:
11+
static int counter;
12+
Test(): a(12), b(45)
13+
{
14+
counter++;
15+
}
16+
17+
static int getCounter()
18+
{
19+
return counter;
20+
}
21+
22+
};
23+
24+
int Test::counter;
25+
26+
int main(void)
27+
{
28+
Test t1;
29+
cout<<t1.counter<<endl;
30+
31+
Test t2;
32+
cout<<Test::counter<<endl;
33+
34+
cout<<t1.getCounter()<<"\t\t"<<Test::getCounter();
35+
36+
37+
return 0;
38+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /