写了下面一小段程序:
#include <iostream> using namespace std;
class test { public: test() { cout << "test constructor\n"; } test(const test&){ cout << "copy constructor\n"; } };
test foo() { test local; return local; }
int main() { test bbb = foo(); return 0; }
用VC6编译运行的结果: test constructor copy constructor
用g++编译运行的结果: test constructor
g++编译器采用了RVO,优化掉了一次拷贝构造函数的执行。
下面两篇文章讲了些RVO的内容: http://community.csdn.net/Expert/FAQ/FAQ_Index.asp?id=175740 http://dev.csdn.net/Develop/article/14/14222.shtm

|