Question about copy constructor

I have this class:

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

and I have a function that contains the following line:

 A *pa1 = new A(a2);

can someone explain what exactly happens when I call the A(a2)compiler calls the constructor or copy constructor, thanks in advance

+3
source share
3 answers

Assuming that it a2is an instance A, this calls the copy constructor.

It will call operator newto get dynamic memory for an object, then it will copy-build a new object into memory, and then return a pointer to that memory.

+5
source

When you call new A (a2);

.
a2.

a2 - int, .

A::A(int initPlayer = 0);

a2 - A, .

A::A(const A&);
+4

a2. int , int, c'tor (A(int player=0)), a2 A , A (.. ), c'tor.

+1

Source: https://habr.com/ru/post/1750011/


All Articles