question about copy constructor

后端 未结 3 1317
伪装坚强ぢ
伪装坚强ぢ 2021-01-20 10:08

I have this class:

class A {
private:
 int player;
public:
 A(int initPlayer = 0);
 A(const A&);
 A& operator=(const A&);
 ~A();
 void foo() cons         


        
3条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-20 11:04

    When you call new A(a2);

    It will call one of the constructors.
    Which one it calls will depend on the type of a2.

    If a2 is an int then the default constructor is called.

    A::A(int initPlayer = 0);
    

    If a2 is an object of type A then the copy constructor will be called.

    A::A(const A&);
    

提交回复
热议问题