[C++] 생성자 멤버변수 초기화

Woozard·2023년 5월 17일
2

C++

목록 보기
1/2
post-thumbnail

생성자에서 멤버변수를 초기화 하는 두 가지 방법


1. this 키워드

class Human {
private:
    int age;
    string name;
public:
	Human (int age, string name) {
		this->age = age;
		this->name = name;	
	}
};

this.이 아니고 this->인 부분을 조심한다.

2. 콜론 초기화

class Human {
private:
    int age;
    string name;
public:
	Human (int age, string name) : age(age), name(name) {
    
	}
};
profile
Hello, World!

0개의 댓글