gpt4 book ai didi

c++ 重载 + 运算符不返回临时对象,为什么?

转载 作者:行者123 更新时间:2023-11-27 23:25:51 27 4
gpt4 key购买 nike

这里是源代码:

#include<iostream>
using namespace std;

class cat{
private:
int a;
public:
cat():a(1){
cout << "const : " << this << endl;
}
~cat(){
cout << "dest : " << this << endl;
}
cat operator+(cat& rhs){
cout << "+" << endl;
cat x;
x.a=a+rhs.a;
return x;
}
cat operator=(const cat& rhs){
cout << "= : " <<this << endl;
a=rhs.a;
return (*this);
}
cat(const cat& rhs){
cout << "copy const : " << this << endl;
a=rhs.a;
}
};

int main(){
cat ob1;
cat ob2;
cat ob3;
ob1=ob2;
cout << "\n 1----1 \n" << endl;
ob3=(ob1+ob2);
cout << "\n 2----2 \n" << endl;
cat ob4=ob1+ob2;
cout << "\n 3----3 \n" << endl;

}

这是输出:

const : 0x22ff20          // ob1 created
const : 0x22ff1c // ob2 created
const : 0x22ff18 // ob3 created
= : 0x22ff20 // calling = operator
copy const : 0x22ff24 // return temporary object using copy constructor
dest : 0x22ff24 // temporary object is destroyed

1 ---- 1

+ // operator + is called
= : 0x22ff2c // it jums to = operator #### (why ?) ####
copy const : 0x22ff28 // = create a temporary object
dest : 0x22ff28 // temporary object created by = is destroyed
dest : 0x22ff2c // x inside + operator is destroyed

// ##################################################
// #### HERE #### copy constructor to create a temporory object
// like what happend in = operator and also destructor of this
// temporary object did not called
// ##################################################

2 ---- 2

+ // here + operator is called
const : 0x22ff14 // x is creted

//######################""
//#### HERE #### copy constructor ob4 that take ob1+ob2 as an
// argument did not get called, why ?
// and also + operator did not return a temporary object and then
// use it as an argument for the copy constructor
//#######################

3 ---- 3

dest : 0x22ff14 // x destroyed
dest : 0x22ff18 // ob3 destroyed
dest : 0x22ff1c // ob2 destroyed
dest : 0x22ff20 // ob1 destroyed

问题从 1 和 2 开始,也从 2 和 3 开始。

所以我的问题在输出中。
在 1 和 2 之间:为什么 + 运算符没有返回一个临时对象然后销毁它发生在 = 运算符中?

在 2 和 3 之间:为什么 + operato 没有返回一个临时对象,该对象将用作复制构造函数中的参数来创建 ob4?

我知道这很长,但我真的很感谢你的帮助。

最佳答案

允许编译器删除(省略)复制结构并就地构建(在最终目的地)。所以它是完全有效的。

PS:这里有个小错误:

cat operator=(const cat& rhs){

应该是:

cat &   operator=(const cat& rhs){
// ^^^

通过这个更正,我得到:

 1----1 

+ // + called.
const : 0x7fff6b29e848 // Local object to + constructed.
// But the return value will be used as a const ref parameter
// to the assignment operator. So we can elide the actual copy
// if we create the temporary object at the destination and use that.
= : 0x7fff6b29e830 // Now we are in the assignment.
// Just copy the value from the temporary object we created as part
// of the optimizations.
dest : 0x7fff6b29e848 // All finished destroy the temporary.

// Note: I use the term temporary very loosely.
// And refer you to the as-is rule.

关于c++ 重载 + 运算符不返回临时对象,为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9594497/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com