函数参数和临时变量之间复制构造函数的奇怪行为
strange behaviour of copy constructor between function parameter and temporary variable
我有例如:
#include <iostream>
using namespace std;
class Point {
private:
int x;
int y;
public:
Point(int i, int j) : x{ i }, y{ j } {
cout << "CTOR POINT: {x=" << x << ", y=" << y << ", addr=" << this << "}" << endl;
}
Point(const Point& p) {
cout << "COPY CTOR from POINT: {x=" << p.x << ", y=" << p.y << ", addr=" << &p << "} to POINT: {x=" << x << ", y=" << y << ", addr=" << this << "}" << endl;
x = p.x;
y = p.y;
}
Point& operator=(const Point& p) {
cout << "OP= from POINT: {x=" << p.x << ", y=" << p.y << ", addr=" << &p << "} to POINT: {x=" << x << ", y=" << y << ", addr=" << this << "}" << endl;
x = p.x;
y = p.y;
return *this;
}
~Point() {
cout << "DTOR POINT: {x=" << x << ", y=" << y << ", addr=" << this << "}" << endl;
}
};
Point func1(Point p) {
cout << "entered in func1 function" << endl;
cout << "exiting in func1 function" << endl;
return p; // p will be copied to temporary variable with Point(const Point&) ctor.
// p will be dtored.
}
int main() {
cout << "entered in main function" << endl;
Point p1{ 5, 10 }; // initialized p1 with Point(int, int) ctor.
p1 = func1(p1); // in call to func1(), p1 arg will be copied to parameter p with Point(const Point&) ctor.
// on return, temporary variable will be assigned to p1 using operator=(const Point&) func.
// temporary variable will be dtored.
Point p2{ 500, 1000 }; // initialized p2 with Point(int, int) ctor.
p1 = p2; // p2 will be assigned to p1 using operator=(const Point&) func.
cout << "exiting in main function" << endl;
// p2 will be dtored.
// p1 will be dtored.
}
,输出为:
entered in main function
CTOR POINT: {x=5, y=10, addr=003EFE38}
COPY CTOR from POINT: {x=5, y=10, addr=003EFE38} to POINT: {x=5, y=10, addr=003EFD28}
entered in func1 function
exiting in func1 function
COPY CTOR from POINT: {x=5, y=10, addr=003EFD28} to POINT: {x=-858993460, y=-858993460, addr=003EFD4C}
DTOR POINT: {x=5, y=10, addr=003EFD28}
OP= from POINT: {x=5, y=10, addr=003EFD4C} to POINT: {x=5, y=10, addr=003EFE38}
DTOR POINT: {x=5, y=10, addr=003EFD4C}
CTOR POINT: {x=500, y=1000, addr=003EFE28}
OP= from POINT: {x=500, y=1000, addr=003EFE28} to POINT: {x=5, y=10, addr=003EFE38}
exiting in main function
DTOR POINT: {x=500, y=1000, addr=003EFE28}
DTOR POINT: {x=500, y=1000, addr=003EFE38}
正如我们所看到的,参数p
和temporary variable
是用复制构造函数构造的。点的成员是建筑类型,因此,如果在构造函数中未指定初始化列表,则不能默认将它们初始化。
问题:
为什么参数 p
在到达Point(const Point&)
构造函数中 cout
之前, x
和 y
具有有效的 CC_4和 CC_5,而 temporary variable
具有垃圾值?
您的复制构造函数未正确实现。每个构造函数(包括复制构造函数(都可以具有成员初始化列表。如果提供,它们将按顺序执行数据成员在班级中被声明。如果未提供,数据成员将被默认初始化。
请记住,数据成员的初始化始终发生在构造函数主体运行之前。
在复制构造函数中,您未能在构造函数初始化列表中初始化数据成员。但是后来您阅读了调用UB的它们。您可能想做:
Point(const Point& p) : x{ p.x }, y{ p.y } {
std::cout << "COPY CTOR from POINT: {x=" << p.x << ", y=" << p.y
<< ", addr=" << &p << "} to POINT: {x=" << x
<< ", y=" << y << ", addr=" << this << "}" << endl;
}
与上述有关,您不应该拥有UB。
demo
相关文章:
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 为什么在C++中使用私有复制构造函数与删除复制构造函数
- 当从函数参数中的临时值调用复制构造函数时
- 如果有一个模板构造函数只有一个泛型参数,为什么我必须有一个复制构造函数
- 使用复制构造函数复制双精度数组
- C 无可行的构造函数复制类型的变量
- 没有可行的构造函数复制类型 'MyString' 的数组元素
- 编译时,复制构造函数/复制分配和正常功能调用优化之间是否存在任何区别
- 如何最小化调用列表构造函数(复制构造函数)的次数?
- C 11矢量构造函数复制与范围
- 我定义了一个非复制构造函数;复制构造函数还会被隐式定义吗
- 可以将构造函数复制为转换运算符
- 将基类指针的构造函数复制到子类
- C++树类:构造函数/复制/内存泄漏
- 如何制作这个在模板构造函数复制中使用类型定义的类型的模板
- 将构造函数复制为模板化的成员函数
- 绕过私有复制构造函数/复制赋值C++
- C++通过构造函数复制对象
- 复制构造函数 - 复制C++中的对象
- 将带unique_ptr的类的构造函数复制到作为成员的抽象类