2
#include <iostream>
using namespace std;
class A {
public:
A() {
cout << "A()" << endl;
}
A(const A& a) {
cout << "A(const A& a)" << endl;
}
A(A&& a) {
cout << "A(A&& a)" << endl;
}
A& operator=(const A& a) {
cout << "operator=(const A& a)" << endl;
return *this;
}
A& operator=(A&& a) {
cout << "operator=(A&& a)" << endl;
return *this;
}
~A() {
cout << "~A()" << endl;
};
};
A foo() {
A a;
return a;
}
int main() {
A a = foo();
}
編譯返回變量:構建和局部變量的超聲破壞和在C++
clang++ test.cpp -o test -std=c++11
輸出:
A()
~A()
爲什麼只有一個對A()和〜A()在輸出?
爲什麼移動構造函數不被調用?
讓編譯器做了一些代碼優化?