본문 바로가기
프로그래밍 언어/C++

[C++] default 생성자 요청

by 까망 하르방 2025. 2. 5.
반응형

default 생성자 요청

클래스에서 아래와 같이 개발자가 기본 생성자를 만들 수 있다.

class Point
{
	int x, y;

public:
	Point() {};
	Point(int a, int b) : x{ a }, y{ b } {}
};

int main()
{
	Point p;
}

 

 

= default」 형태로

컴파일러에게 기본 생성자를 만들어 달라고 요청할 수 있다.

이는 최적화를 보다 잘 지원 받기 위함

class Point
{
	int x, y;

public:
	Point() = default;
	Point(int a, int b) : x{ a }, y{ b } {}
};

int main()
{
	Point p;
}

 

 

📌 [C++] 생성자 함수

 

[C++] 생성자 함수

생성자 함수 (constructor)• 클래스 이름과 동일한 이름의 멤버함수• 반환(return) 타입을 명시할 수 없다.• 객체 생성시 단 한번 호출된다. (초기화 역할)• 오버로딩(overloading) 가능• 디폴트 값

zoosso.tistory.com

 

 

📌 [C++] 복사 생성자

 

[C++] 복사 생성자 (Copy Constructor)란?

복사 생성자 (Copy Constructor)란?객체가 자신과 동일한 타입의 객체로 초기화 될 때 호출되는 생성자class A{ int no; char ch;public: A(const A & ra); // 복사생성자함수};  [예시] 복사 생성자p2는 Point(int) 필

zoosso.tistory.com

 

반응형

댓글