求教一个 c++语法问题, 这段代码为何只析构一次呢
資深大佬 : jdz 2
class excption_test
{
public:
excption_test();
~excption_test();
};
{
public:
excption_test();
~excption_test();
};
excption_test::excption_test()
{
std::cout << “in constructorn”;
}
excption_test::~excption_test()
{
std::cout << “auto in destructor??n”;
}
excption_test get_ex()
{
excption_test c;
return c;
}
int main()
{
excption_test m = get_ex();
return 0;
}
我理解 get_ex()函数中创建了一个对象, 离开栈的时候会进行析构, 析构 c 对象, 同时调用拷贝构造函数拷贝给 main 函数中的 m 变量, 在 main 函数结束的时候再析构 m 对象
大佬有話說 (11)