본문 바로가기
Language/C++

09.this-const-static-friend

by 아무키 2023. 12. 22.
반응형

this 포인터 (this Pointer)

this는 C++ 키워드

멤버 함수를 호출한 객체의 주소 값

 

사용 예

void Player::SetPosition(int x, int y){
	x = x;
	y = y; //명확하지가 않다.
}

void Player::SetPosition(int x, int y){
	this -> x = x;
  this -> y = y; 
	// 좌변은 변수, 우변은 인자로 명확
}

//ex 2

class Player{
private:
	int x, y;
	int speed;
public:
	Player(int x, int y, int speed){
		: x{x}, y{y}, speed{speed};
	{
	cout << this << endl;
	}
};

int main(){
	Player* p = new player{1, 1, 1};
	cout << p << endl;
}

//ex 3

void Player::ComparePosition(const Player& other){
	if(this == &other){
		cout << "Smae Position" << endl;
	}
	player1.ComparePosition(player1);

 

 

const 객체 (const object)

const-corretness

const 객체의 멤버 변수의 값은 변경 불가능

class Player{
	private:
		int x, y;
		int speed;
	public:
		Player(int x, int y, int speed){
			: x{x}, y{y}, speed{speed}
		{
			cout << this << endl;
		}
		void SetPosition(int x, int y){
			this -> x = x;
			this -> y = y;
		}
		void PrintPosition(){
			cout << x << ' ' << y << endl;
};

int main(){
	const Player p{1, 1, 1};
	p.Setposion(2, 2); // error
}
//p가 const 객체이기 때문에, 멤버 변수의
//값을 바꾸는 Setposion() 멤버 함수는
//컴파일러가 호출하지 못하도록 오류를 발생

//PrintPosition()함수는 멤버 변수인
//x와 y의 값을 바꾸지 않으나 오류 발생
//컴파일러는 PrintPosition()함수 안에서
//값을 바꾸는지 모름

 

값을 변경하지 않는다는 것을 명시적으로 알려주어야함

멤버 함수 선언 뒤에 const

void PrintPosition()const{
	cout <<x << ' ' << y << endl;
};

 

static 클래스 멤버 변수 (static class members)

static 클래스 멤버 변수

객체가 아닌 클래스에 속하는 변수

개별적인 객체의 데이터가 아닌 클래스에 공통 데이터 구현이 필요할 때 사용

 

static 클래스 멤버 함수

객체가 아닌 클래스에 속하는 함수

클래스 이름 하에서 바로 호출 가능

Static 클래스 멤버 함수는 static 클래스 멤버 변수에만 접근 가능

 

static 클래스 멤버

class Player{
	private:
		static int n_players;
	public:
		static int get_n_players();
};

int Player::n_players = 0;

int Player::get_n_players(){
	return n_players;
};

 

n_players를 증가시키는 위치에 주의

→ 생성자 위임 등의 구현에서 중복적으로 플레이어 숫자를 증가시키지 않도록 잘 설계

Player::Player(int x, int y, int speed){
	: x{x}, y{y}, speed{speed}
{
	n_players++;
}
Player:~Player()
{
	n_player--;
}

 

Static 변수는 stack과 heap이 아닌 별도 메모리 공간에 저장된다.

메모리 : 코드공간 | static 변수, 전역 변수, String 리터럴 |스택(Stack) | 힙(Heap)

 

friend 키워드(Friends of a keyword)

private 멤버에 대해 접근할 수 있는 함수나 클래스를 선언할 때 사용

 

비대칭

→ A가 B의 friend일 때 B는 A의 friend는 아님

전이되지 않음

→ A가 B의 friend이고 B가 C의 friend일때, A가 C의 friend는 아님

class Player
{
	friend void DisplayerPlayer(const Player& p);

private:
	int x, y, speed;
public:
	Player(int x, int y, int speed){
		:x{x}, y{y}, speed{speed};
	{
		cout << this << endl;
	}
	void SetPosition(int x, int y){
		this -> x = x;
		this -> y = y;
	}
};

void DisplayPlayer(const Player& p){
	cout << p.x << ", " << p.y << endl;
}
// friend가 아니면 private에 접근 불가능
// Player::DisplayPlayer()가 아닌것 확인

 

friend 클래스 (friends of a class)

class Player{
	friend class Other_class;
private:
	int x, y;
	int speed;
public:
...
};
반응형

'Language > C++' 카테고리의 다른 글

11.다형성  (1) 2023.12.25
10.상속  (0) 2023.12.23
08.생성자와 소멸자  (0) 2023.12.22
07.OOP  (0) 2023.12.22
06.참조자  (0) 2023.12.22

댓글