#include <iostream>
using namespace std;
class Car
{
public:
~Car() { cout << "Car is destructed." << endl; }
};
class Taxi :public Car
{
public:
~Taxi() {cout << "Taxi is destructed." << endl; }
};
void test(Car c) {}
int main()
{
Taxi taxi;
test(taxi);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是输出:
Car is destructed.
Car is destructed.
Taxi is destructed.
Car is destructed.
Run Code Online (Sandbox Code Playgroud)
我使用的是MS Visual Studio Community 2017(对不起,我不知道如何查看Visual C ++版本)。当我使用调试模式时。我发现在void test(Car c){ }按预期方式离开函数主体时执行了一个析构函数。而当一个额外的析构函数出现test(taxi);结束了。
该 …