在以下代碼:這是誰的析構函數?
#include <string>
#include <iostream>
struct S {
std::string a;
int b;
S(const S&) = default;
S(S&&) = default;
S& operator=(const S&) = default;
S& operator=(S&&) = default; // not required here but should be added for completeness
~S() {
std::cout << a << " " << b << std::endl;
}
};
S f(S arg) {
S s0{};
S s1(s0); //s1 {s0}; in the book
s1 = arg;
return s1;
}
int main()
{
S s3{"tool",42};
f(s3);
}
我得到以下輸出(I評論與我的推理輸出):
42 //???
0 // s0 is destroyed
tool 42 // arg is destroyed
tool 42 // return value of f() is destroyed
tool 42 // s3 is destroyed
其析構函數是輸出42的一個??我不明白
我似乎無法重現此。你能告訴我們你正在使用哪個平臺,編譯器和編譯命令行嗎? – SirDarius 2014-11-04 11:11:07
[cnr](http://ideone.com/qDoRSg)。 – WhozCraig 2014-11-04 11:11:17
除了第一行中的「42」之外,我都可以得到所有的東西。你已經肯定使用C++ 11出現,整個 「默認」 的經營.... 語言: C++ 11創建 : 0秒前 知名度: 公共\t 分享或嵌入源代碼的 – ha9u63ar 2014-11-04 11:15:41