Machineboy空

Object-Oriented Data Structures # WEEK03 : Constructors - default, copy & copy assignment operator 본문

Computer/자료구조

Object-Oriented Data Structures # WEEK03 : Constructors - default, copy & copy assignment operator

안녕도라 2024. 2. 2. 12:03

보다 구조적으로, c++ 하단에서 어떤일이 일어나는지 보여준다.

무심코 썼던 함수라거나, 전역변수를 초기화하지 않았을 때 디폴트값이 설정되는 것등을 해주는 보이지 않는 녀석들에 관한 이야기.

클래스 생성자, 자동 복사 생성자 등

 

생성자는 기본 생성자가 있고 복사해서 생성하는 방법이 있다. 

그리고 복사해서 생성할 때 변수 공간에 할당하는 연산자를 알아볼 것.

 

 

3.1 Class Constructor

 

When an instance pf a class is created, the class constructor sets up the initial state of the object.

ex. Default : Unit Cube(d=1)


Automatic Default Constructor

If we do not provide any custom constructors, the C++ compiler provides an automatic default constructor for our class for free!

 

The automatic default constructor will only initialize all member variables to their default values.

 

* because no constructors present, automatic default constructor 

생성자가 없으면 자동 디폴트로 생성자가 생긴다.

 

Custom Default  Constructor

 

The simplest constructor we can provide is a custom default constructor that specifies the state of the object when the object is constructed. We define one by creating:

세 가지 요건을 모두 만족해야 사용자 지정 기본 생성자 정의 가능

  • A member function with the same name of the class
  • The function takes zero parameters
  • The function does not have a return type.
Cube::Cube() // custom default constructor

 

Custom  Constructor

 

We can also specify custom, non-default constructors that require client code to supply argument:

Cube::Cube(double length) // one-argument ctor specifying initial length

 

If any custom constructor is defined, an automatic defualt constructor is not defined.

동시에 존재할 수 없음. 어떤 커스텀 생성자가 정의되면 자동 기본 생성자가 정의되는 것을 방지할 수 있다.

만약 둘 다 없다면 컴파일러가 에러를 발생시킬 것

 

C++ Constructor, set up the initial value of the class

In almost class we build, we going to define constructors so that we can control state of the class.


3.2 Copy Constructor

 

In C++, a copy constructor is a special constructor that exists to make a copy of an existing object.

 

Automatic Copy Constructor

If we do not provide a custom copy constructor, the C++ compiler provides an automatic copy constructor for our class for free!

 

The automatic copy constructor will copy the contents of all member variables.

 

Custom Copy Constructor

A custom copy constructor is:

  • A class constructor
  • Has exactly one argument
    • The argument must be const reference of the same type as the class.
Cube::Cube(const Cube & obj)

 

복사 생성자는 거의 모든 경우에 자동으로 호출된다.

 

Copy Constructor Invocation

Often, copy constructors are invoked automatically:

  • Passing an object as a parameter(by value)
  • Returning an object from a function (by value)
  • Initializing a new object

1 main에서 foo()호출
2 foo()에서 Cube c 생성 시, default Constructor 호출
3 foo()에서 main으로 c return할 때 copy Constructor 호출
4 main에서 c2라는 변수에 c를 대입할 때 copu Constructor 호출

 

1 Cube c; Cube myCube; -> default constructor로 cube 생성
2 myCube = c; 여기는 copy Constructor가 호출될 것으로 예상되지만
이미 다른 default constructor로 생성된 상태로 copy Constructor가 호출되지 않는다.
  대신 assignment operator가 호출되는 것!
이미 생성된 것을 대입하는 것이니까

 


3.3 Copy Assignment Operator

 

코딩에서의 연산자 = 는 대입연산자! 복사해서 넣어줌.

 

In C++, a copy assignment operator defines the behavior when an object is copied using the assignment operator =

 

Copy Constructor vs Assignment

 

복사 생성자는 새로운 것을 만들고, 할당 연산자는 기존에 만들어진 것을 대입.

 

A copy constructor creates a new object (constructor).

 

A assignment operator assigns a value to an existing object.

  • An assignment operator is always called on an object that has already been constructed

 

Automatic Assignment Operator

If an assignment operator is not provided, the C++ compiler provides an automatic assignment operator.

 

The automatic assignment operator will copy the contents of all member variables.

 

A custom assignment operator is:

  • Is a public member function of the class.
  • Has the function name operator =
  • Has a return value of a reference of the class' type.
  • Has exactly one argument
    • The argument must be const reference of the class' type
Cube & Cube:: operator = (const Cube & obj)