정적 멤버
- class에는 속하지만, 객체 별로 할당되지 않고 클래스의 모든 객체가 공유하는 멤버
- 즉, 클래스의 모든 객체에 대해 하나의 데이터만 유지됨
#include<iostream>
using namespace std;
class Something{
public:
static int s_value;
};
int Something::s_value = 1024;
int main(){
Something s1;
cout << s1.s_value << endl;
return 0;
}
정적 멤버 함수
- 클래스의 객체를 생성하지않고 호출 가능
- 객체를 생성하지 않으므로, this 포인터를 가지지 않음
- 특정 객체에 포함되지 않으므로, 정적 멤버 변수만 사용가능
#include <iostream>
using namespace std;
class Something{
private:
static int s_value;
public:
static int getValue(){
return s_value;
}
int temp(){
return this->s_value;
}
};
int Something::s_value = 1024;
int main(){
cout << Something::getValue() <<endl;
Something s1, s2;
cout << s1.getValue() << endl;
int (Something::*fptr1)() = &Something::temp;
cout << (s2.*fptr1)() <<endl;
int (*fptr2)() = &Something::getValue;
cout << (*fptr2)() << endl;
return 0;
}
'c++' 카테고리의 다른 글
메모리의 stack영역, heap영역 (0) | 2023.02.16 |
---|---|
함수 포인터 (0) | 2023.02.15 |
함수 오버로딩 (0) | 2023.02.11 |