我试图理解这种分配在C是如何工作++:C++简单的分配由函数
Test other = toto();
这是一个完整的代码来源:
#include <iostream>
class Test
{
public:
Test()
{
j = i++;
std::cout<<"default constructor "<<j<<std::endl;
}
Test(const Test&)
{
std::cout<<"constuctor by copy "<<j<<std::endl;
}
Test & operator=(const Test&)
{
std::cout<<"operator = "<<j<<std::endl;
return *this;
}
int j;
static int i;
};
int Test::i = 0;
Test toto()
{
Test t;
return t;
}
int main()
{
Test other = toto();
std::cout<<other.j<<std::endl;
Test another;
return 0;
}
的代码不会被复制或操作员使用的构造=,所以我不明白它有多么的作品...... 我用gcc 4.7.0
Thranks您的帮助:)
杰罗姆
@jrok - 从FAQ“他们的一位工程师告诉我,他们发现这个回报率按价值优化是如此之快,你得到它,即使你不打开优化编译”,因此显然这不是一定是真的。它可能因编译器而异。 – Benj
@Benj事实上,在使用gcc进行尝试的情况下,即使没有优化也没有副本。 – jrok