有关复制构造函数的问题 [英] question about copy constructor

查看:47
本文介绍了有关复制构造函数的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我上了这个课:

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

我有包含此行的函数:

 A *pa1 = new A(a2);

当我调用 A(a2)时,编译器调用复制构造函数或构造函数,请先解释一下究竟是怎么回事,

can somebody please explain what exactly is going on, when I call A(a2) compiler calls copy constructor or constructor, thanks in advance

推荐答案

假定 a2 A 的实例,这将调用复制构造函数.

Assuming a2 is an instance of A, this calls the copy constructor.

它将调用 operator new 来获取对象的动态内存,然后它将新对象复制-构造到内存中,然后返回指向该内存的指针.

It will call operator new to get dynamic memory for the object, then it will copy-construct a new object into the memory, then return a pointer to that memory.

这篇关于有关复制构造函数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆