[C++] 클래스의 배열, this 포인터

dd_ddong·2022년 7월 18일
0

c++

목록 보기
18/38

클래스의 배열

1. 객체 배열

Person arr[10];
Person *arr = new Person[10];
객체로 이뤄진 배열, 배열 생성시 객체가 생성된다. 이 때 각 객체 생성 시 void 형 생성자가 호출되므로 꼭 있어야 한다.

2. 클래스 포인터 배열

Person *arr[10];
Person타입의 포인터로 이뤄진 배열, 별도로 객체 생성해줘야 한다.

this 포인터

자기자신을 가리키는 포인터

class Person
{
private:
	const char *name;
    int age;
public:
	Person(const char &name, int age){
    	this->name = &name;
        this->age = age;
    }
}

Person 생성자 안에서 this->name은 Person class의 멤버변수 name이고
name은 생성자의 매개변수 name이다.

0개의 댓글