Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/154.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
奇怪的复制构造函数 以下C++程序: #include <memory> #include <iostream> using namespace std; struct my_class{ int value; my_class(int id): value(id){ cout<<"constructing "<<id<<endl; cout<<"address is "<<static_cast<const void *>(this)<<endl; } my_class(const my_class & a){ cout<<"construct copying "<<a.value<<endl; cout<<static_cast<const void *>(this)<<"<-"<<static_cast<const void *>(&a)<<endl; } my_class operator=(const my_class & a){ cout<<"assignment copying "<<a.value<<endl; this->value = a.value; cout<<static_cast<const void *>(this)<<"<-"<<static_cast<const void *>(&a)<<endl; return *this; } ~my_class(){ cout<<"deleting "<<this->value<<endl; cout<<"address is "<<static_cast<const void *>(this)<<endl; } }; my_class f(){ cout<<"==in f=="<<endl; my_class temp(2); cout<<"==out f=="<<endl; return temp; } int main(){ cout<<"==in main=="<<endl; my_class a(1); a = f(); a.value++; cout<<"==out main=="<<endl; return 0; } #包括 #包括 使用名称空间std; 构造my_类{ int值; my_类(int id):值(id){ cout_C++_Copy Constructor_Copy Assignment - Fatal编程技术网

奇怪的复制构造函数 以下C++程序: #include <memory> #include <iostream> using namespace std; struct my_class{ int value; my_class(int id): value(id){ cout<<"constructing "<<id<<endl; cout<<"address is "<<static_cast<const void *>(this)<<endl; } my_class(const my_class & a){ cout<<"construct copying "<<a.value<<endl; cout<<static_cast<const void *>(this)<<"<-"<<static_cast<const void *>(&a)<<endl; } my_class operator=(const my_class & a){ cout<<"assignment copying "<<a.value<<endl; this->value = a.value; cout<<static_cast<const void *>(this)<<"<-"<<static_cast<const void *>(&a)<<endl; return *this; } ~my_class(){ cout<<"deleting "<<this->value<<endl; cout<<"address is "<<static_cast<const void *>(this)<<endl; } }; my_class f(){ cout<<"==in f=="<<endl; my_class temp(2); cout<<"==out f=="<<endl; return temp; } int main(){ cout<<"==in main=="<<endl; my_class a(1); a = f(); a.value++; cout<<"==out main=="<<endl; return 0; } #包括 #包括 使用名称空间std; 构造my_类{ int值; my_类(int id):值(id){ cout

奇怪的复制构造函数 以下C++程序: #include <memory> #include <iostream> using namespace std; struct my_class{ int value; my_class(int id): value(id){ cout<<"constructing "<<id<<endl; cout<<"address is "<<static_cast<const void *>(this)<<endl; } my_class(const my_class & a){ cout<<"construct copying "<<a.value<<endl; cout<<static_cast<const void *>(this)<<"<-"<<static_cast<const void *>(&a)<<endl; } my_class operator=(const my_class & a){ cout<<"assignment copying "<<a.value<<endl; this->value = a.value; cout<<static_cast<const void *>(this)<<"<-"<<static_cast<const void *>(&a)<<endl; return *this; } ~my_class(){ cout<<"deleting "<<this->value<<endl; cout<<"address is "<<static_cast<const void *>(this)<<endl; } }; my_class f(){ cout<<"==in f=="<<endl; my_class temp(2); cout<<"==out f=="<<endl; return temp; } int main(){ cout<<"==in main=="<<endl; my_class a(1); a = f(); a.value++; cout<<"==out main=="<<endl; return 0; } #包括 #包括 使用名称空间std; 构造my_类{ int值; my_类(int id):值(id){ cout,c++,copy-constructor,copy-assignment,C++,Copy Constructor,Copy Assignment,这是因为赋值运算符返回对象的副本。此处: my_class operator=(const my_class & a) 请改用引用: my_class& operator=(const my_class & a) 这是因为赋值运算符返回对象的副本。此处: my_class operator=(const my_class & a) 请改用引用: my_class& operator=(const my_class & a) 调用复制

这是因为赋值运算符返回对象的副本。此处:

 my_class operator=(const my_class & a)
请改用引用:

 my_class& operator=(const my_class & a)

这是因为赋值运算符返回对象的副本。此处:

 my_class operator=(const my_class & a)
请改用引用:

 my_class& operator=(const my_class & a)

调用复制构造函数是因为赋值运算符返回了
*this
的副本。正如其他人所指出的,赋值运算符应该返回引用而不是副本;这既可以避免不必要的复制,也可以允许对运算符进行链接

也许您想问的问题是,为什么从赋值运算符返回的值包含一个副本,而从
f()
返回的值不包含副本

f()
正在返回本地对象的副本,从函数返回后,该副本不需要保留。这允许编译器执行返回值优化,其中要返回的变量存储在调用方可访问的位置,并成为返回值,而无需复制或移动它

operator=()
正在返回持久对象的副本。由于原始对象仍将存在,并且必须与返回的值分开,因此此处需要一个副本


或者您可能想问,既然复制的对象从未使用过,为什么编译器不消除副本?这是因为您已经给出了构造函数和析构函数的副作用,编译器不允许消除这些副作用。

调用副本构造函数是因为赋值运算符返回了
*this的副本de>。正如其他人所指出的,赋值运算符应该返回引用而不是副本;这既可以避免不必要的复制,也可以允许将运算符链接起来

也许您想问的问题是,为什么从赋值运算符返回的值包含一个副本,而从
f()
返回的值不包含副本

f()
正在返回本地对象的副本,从函数返回后,该副本不需要保留。这允许编译器执行返回值优化,其中要返回的变量存储在调用方可访问的位置,并成为返回值,而无需复制或移动它

operator=()
正在返回持久对象的副本。由于原始对象仍将存在,并且必须与返回的值分开,因此此处需要一个副本



或者,您可能想问,既然复制的对象从未使用过,为什么编译器不消除副本?这是因为您已经给出了构造函数和析构函数的副作用,编译器不允许消除这些副作用。

您的输出与代码中的日志记录不匹配。此代码具有未定义的行为,因为您通过复制构造函数创建对象时,使用
value
而不初始化它。从不使用复制构造函数创建的对象。正如您所看到的,只要程序离开复制赋值,对象就会被销毁。顺便说一下,我没有提供默认构造函数,为什么可以在不使用复制构造函数的情况下创建对象“您的输出与代码中的日志记录不匹配。”--是的,我更新了程序,但忘记重新运行程序。我已经更新了结果。复制构造函数不调用默认构造函数。这里没有链接,并且这种链接仅在C++11中允许,请参阅委派构造函数。您的输出与代码中的日志记录不匹配。此代码具有未定义的行为,因为您是我们通过复制构造函数创建对象时,不初始化
value
。从不使用复制构造函数创建的对象。正如您所看到的,只要程序离开复制赋值,对象就会被销毁。顺便说一下,我没有提供默认构造函数,为什么可以在不使用复制构造函数的情况下创建对象初始化。“您的输出与代码中的日志记录不匹配。”--是的,我更新了程序,但忘了重新运行程序。我已经更新了结果。复制构造函数不调用默认构造函数。这里没有链接,这种链接只有在C++11中才允许,请参阅委派构造函数。是的,我知道在实际项目中我应该使用我的_类&。但我只是好奇这个p的行为由于我没有提供默认构造函数,为什么可以创建具有未初始化值的对象?我无法想象程序运行时会发生什么。您提供了复制构造函数:
my_class(const my_class&a)
,但是它(您的代码)不将值复制到新构造的对象中。我明白你的意思。但你认为对复制构造函数的调用完全没有必要吗?默认构造函数创建的对象在创建后立即被销毁。我不知道如何表达这一点,但我的观点是,既然创建的对象类似于临时对象,为什么编译器并没有出于优化目的而消除它。@RainSia:创建和销毁对象有副作用,因为您在构造函数和析构函数中执行输出。编译器不允许消除这些副作用。是的,我知道在实际项目中我应该使用我的_类&。但我只是好奇这个程序的行为。S由于我没有提供默认构造函数,为什么可以创建具有未初始化值的对象?我无法想象程序运行时会发生什么。您提供了复制构造函数:
my_类(const my_class&a)
,但是它(您的代码)不将值复制到新构造的对象中。我明白你的意思。但你认为对复制构造函数的调用完全没有必要吗?默认构造函数创建的对象在创建后立即被销毁。我不知道如何表达这一点,但我的观点是