#include <iostream>
#include <vector>
#include <string.h> using namespace std; class CTest {
public:
CTest() {
cout << "CTest construct" << endl;
data = new char[];
strncpy(data, "hello", );
} CTest(const CTest& test) {
cout << "copy contruct" << endl;
data = new char[];
memcpy(data, test.data, );
} ~CTest() {
cout << "CTest destruct" << endl;
delete data;
data = NULL;
} public:
char *data;
}; CTest GetTest() {
CTest test;
return test;
} int main(int argc, char** argv) { GetTest();
return ;
}
执行结果如下:
并没有进行对象拷贝操作,与预期不符,Why?
其原因是:RVO(return value optimization),被G++进行值返回的优化了,具体的RVO的相关技术,可以进行百度。
我们可以将RVO优化关闭,可以对g++增加选项-fno-elide-constructors,重新编绎之后,
执行结果如下:
转载自:https://blog.csdn.net/dreamvyps/article/details/79154321