只是为了好奇和实验,我写了下面的代码,现在我想了解删除后发生的事情...为什么猫对象还在喵喵?
我使用的编译器版本:
g++ (Ubuntu 5.4.0-6ubuntu1~16.04.9) 5.4.0 20160609
Run Code Online (Sandbox Code Playgroud)
并编译代码:
g++ cat.cpp -pedantic -Wall -o cat
Run Code Online (Sandbox Code Playgroud)
在删除后调用meou()时,其他编译器可能会崩溃.
我想知道
代码:
#include <iostream>
using namespace std;
class Cat
{
public:
Cat() { cout << "Cat construct" << endl; }
~Cat() { cout << "Cat destruct" << endl; }
void meow();
};
void Cat::meow(void)
{
cout << "meow..." << endl;
}
int main()
{
Cat * pCat = new Cat;
pCat->meow();
cout << "pCat = " << pCat << endl;
delete pCat;
pCat …Run Code Online (Sandbox Code Playgroud)