Programming/[C++]
[C++] Static (스태틱)
iD이드
2018. 3. 21. 16:27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
|
#include <iostream>
using namespace std;
/*
static method
1. class를 통틀어서 오직 하나다
2. this pointer가 존재하지 않는다
3. instance field를 사용할 수 없다
4. 클래스명::메소드() 이렇게 사용도 할 수 있고,
객체 메소드() *p 객체를 만든 후에 가능
*/
class A {
int a;
public :
A() {
a = 100;
}
static void disp(A *th) {
cout << "static method" << endl;
cout << th ->a << endl;
}
};
void main() {
//A::disp();
A aa;
aa.disp(&aa);
}
/* Java 스럽게 만든 코드(잘 사용하지 않음)
class A {
int a;
public:
A() {
a = 100;
}
static void disp(A &th) {
cout << "static method" << endl;
cout << th.a << endl;
}
};
void main() {
//A::disp();
A aa;
aa.disp(aa);
}*/
/*
class A {
static int a;
public:
A() {
a = 100;
}
static void disp() {
cout << "static method" << endl;
cout << a << endl;
}
};
int A::a = 300;
void main() {
A::disp();
//A aa;
//aa.disp(aa);
}*/
|
cs |