这是我错过的东西,但我很惊讶。考虑下面的代码示例:为什么在按值传递右值时调用的复制构造函数不起作用
#include <iostream>
class A
{
int a;
public:
A(int a) : a(a) { std::cout << "Normal constructor called." << std::endl; }
A(const A& orig) : a(orig.a) { std::cout << "Copy constructor called." << std::endl; }
};
void testFunction1(const A arg) { std::cout << "testFunction1()" << std::endl; }
void testFunction2(const A& arg) { std::cout << "testFunction2()" << std::endl; }
int main()
{
testFunction1(A(2));
testFunction2(A(2));
return 0;
}
我希望以下结果:
/* Normal constructor called. */
/* Copy constructor called. */
/* testFunction1() */
/* Normal constructor called. */
/* testFunction2() */
但是我错了。确切的结果如下:
/* Normal constructor called. */
/* testFunction1() */
/* Normal constructor called. */
/* testFunction2() */
为什么不叫当我走过A(2)
按值testFunction1()
拷贝构造函数?这是否意味着在C++ 98中通过值或引用传递右值没有区别?这是一种优化吗?是A(2)
和arg
完全一样对象在testFunction1()
?
可能是[* copy elision *](https://stackoverflow.com/questions/12953127/what-are-copy-elision-and-return-value-optimization)。 –
@Someprogrammerdude - 谢谢,我会检查它。从来没有听说过。 –
如果你从来没有听说过这个,那么你可能会阅读一些很好的C++书籍......强烈推荐** Meyers **和** Sutter **书籍。 – Phil1970