반응형
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++] 생성자 함수
생성자 함수 (constructor)• 클래스 이름과 동일한 이름의 멤버함수• 반환(return) 타입을 명시할 수 없다.• 객체 생성시 단 한번 호출된다. (초기화 역할)• 오버로딩(overloading) 가능• 디폴트 값
zoosso.tistory.com
[C++] 복사 생성자 (Copy Constructor)란?
복사 생성자 (Copy Constructor)란?객체가 자신과 동일한 타입의 객체로 초기화 될 때 호출되는 생성자class A{ int no; char ch;public: A(const A & ra); // 복사생성자함수}; [예시] 복사 생성자p2는 Point(int) 필
zoosso.tistory.com
반응형
'프로그래밍 언어 > C++' 카테고리의 다른 글
[C++] class this 키워드 (8) | 2025.02.08 |
---|---|
[C++] static 변수 필요성 및 활용 (5) | 2025.02.06 |
[C++] 복사 생성자 (Copy Constructor)란? (3) | 2025.02.04 |
[C++] default member initializer (1) | 2025.02.02 |
[C++] 클래스 파일 분할 (4) | 2025.01.30 |
댓글