复制构造函数
Copy Constructor
class Gun{
private:
int bullet;
public:
Gun(int bnum) : bullet(bnum) { }
};
class Police{
private:
Gun * pistol;
public:
Police(int bNum) : {
if(bNum>0)
pistol = new Gun(bNum);
else
pistol=NULL;
}
Police(const Police& ref){
pistol=new Gun(*(ref.pistol)); //Confused about this part.
}
};
我现在正在学习C++,我对警察的复制构造函数中发生的事情有点迷茫。我相信 Gun 的构造函数只接受整数,但是如何将 *(ref.pistol) 作为参数分配给它?我认为*(ref.pistol)是一个Gun对象,而不是一个整数。
如果您自己没有显式声明复制构造函数,编译器总是隐式声明一个复制构造函数(尽管在某些情况下可以将其删除)。您对此感到困惑的行调用此隐式声明的复制构造函数。
这个隐式声明的复制构造函数是公共的,(如果使用的话)它只是做一个成员复制,即,就好像你已经编写了
public:
Gun(const Gun& other): bullet(other.bullet) {}
我相信 Gun 的构造函数只接受整数,但是如何将 *(ref.pistol) 作为参数分配给它?我认为*(ref.pistol)是一个Gun对象,而不是一个整数。
你是对的,*(ref.pistol)是一把枪,而不是一个整数。C++允许您使用某些特殊方法而不声明它们,编译器将为您生成一些(希望)合适的方法。复制构造函数就是其中之一。
在警察的复制构造函数中,使用默认的 Gun 复制构造函数创建一个新Gun
,然后将其分配给this->pistol
。
如果你想明确地说你想要默认的复制构造函数,你可以在 Gun 中写这个:
Gun(const Gun& other) = default;
这同样适用于构造函数、析构函数、复制/移动构造函数和赋值运算符。
*(ref.pistol)
正在访问Gun
实例,因此代码正在尝试调用Gun
的复制构造函数,但您尚未显式定义一个。 您仅定义了非默认非复制构造函数,因此编译器可能会省略创建默认复制构造函数。 不过,您可以显式定义自己的复制构造函数:
class Gun{
private:
int bullet;
public:
Gun(int bnum) : bullet(bnum) { }
Gun(const Gun& ref) : bullet(ref.bullet) { }
};
或者,在 C++11 及更高版本中:
class Gun{
private:
int bullet;
public:
Gun(int bnum) : bullet(bnum) { }
Gun(const Gun& ref) = default;
};
无论哪种方式,您都可以像这样使用它(不要忘记pistol
可以是 NULL,因此您必须在 Police
复制构造函数中检查它):
class Police{
private:
Gun * pistol;
public:
Police(int bNum) {
if(bNum>0)
pistol = new Gun(bNum);
else
pistol=NULL;
}
Police(const Police& ref) {
if (ref.pistol)
pistol=new Gun(*(ref.pistol));
else
pistol=NULL;
}
~Police() {
delete pistol;
}
};
并且不要忘记将类似的逻辑应用于复制赋值运算符(在处理手动深度复制实现时,不要忘记 3 法则和 C++11 及更高版本中的 5 法则):
class Gun{
private:
...
public:
...
// the implicit operator is sufficient in this particular example,
// this is being shown just for demonstation purposes...
Gun& operator=(const Gun& ref) {
bullet = ref.bullet;
return *this;
}
// or, in C++11 and later
//Gun& operator=(const Gun& ref) = default;
};
class Police{
private:
...
public:
...
Police& operator=(const Police& ref) {
if (ref != *this) {
delete pistol;
pistol = NULL;
if (ref.pistol)
pistol=new Gun(*(ref.pistol));
}
return *this;
}
// alternatively:
/*
Police& operator=(const Police& ref) {
if (ref != *this) {
if (pistol) {
if (ref.pistol) {
*pistol = *(ref.pistol);
return *this;
}
delete pistol;
pistol = NULL;
}
if (ref.pistol)
pistol=new Gun(*(ref.pistol));
}
return *this;
}
*/
};
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 当从函数参数中的临时值调用复制构造函数时
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 使用复制构造函数复制双精度数组
- C 无可行的构造函数复制类型的变量
- 没有可行的构造函数复制类型 'MyString' 的数组元素
- 编译时,复制构造函数/复制分配和正常功能调用优化之间是否存在任何区别
- 如何最小化调用列表构造函数(复制构造函数)的次数?
- C 11矢量构造函数复制与范围
- 我定义了一个非复制构造函数;复制构造函数还会被隐式定义吗
- 可以将构造函数复制为转换运算符
- 将基类指针的构造函数复制到子类
- C++树类:构造函数/复制/内存泄漏
- 如何制作这个在模板构造函数复制中使用类型定义的类型的模板
- 将构造函数复制为模板化的成员函数
- 绕过私有复制构造函数/复制赋值C++
- C++通过构造函数复制对象
- 复制构造函数 - 复制C++中的对象
- 将带unique_ptr的类的构造函数复制到作为成员的抽象类