不應該「這裏的」被打印之前臨時A(3)
被破壞?臨時A(3)不應該在打印「Here」之前銷燬?
#include <iostream>
struct A
{
int a;
A() { std::cout << "A()" << std::endl; }
A(int a) : a(a) { std::cout << "A(" << a << ")" << std::endl; }
~A() { std::cout << "~A() " << a << '\n'; }
};
int main()
{
A a[2] = { A(1), A(2) }, A(3);
std::cout << "Here" << '\n';
}
輸出:
A(1)
A(2)
A(3)
Here
~A() 3
~A() 2
~A() 1
不,一旦對象超出範圍(即達到main的'}'時,析構函數就會被調用)。 – 101010
@ 101010是不是臨時在聲明'的端部被破壞A中的[2] = {A(1),A(2)},A(3);'? –