#include "stdafx.h"
#include "iostream"
using namespace std;
class A{
private:
int a;
public:
A(int b = 10):a(b){cout<<"struct"<<endl;}
~A(){cout<<"destruct"<<endl;}
void start() {
cout<<a<<endl;
}
void set(int n) {
a = n;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
A* b;
{
A a(2);
b = &a;
b->start();
return 0;
#include "iostream"
using namespace std;
class A{
private:
int a;
public:
A(int b = 10):a(b){cout<<"struct"<<endl;}
~A(){cout<<"destruct"<<endl;}
void start() {
cout<<a<<endl;
}
void set(int n) {
a = n;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
A* b;
{
A a(2);
b = &a;
b->start();
}
//此时a已经被析构了。
b->set(3);b->start(); //这里a已经被析构了,为什么这里不报错呢!?
//析构的时候,为了效率,并不会将内存清0。这样的话。对象的空间仍在那里放着,只是别人现在能够用了。\
如果内部没有得破坏,指针依然指向那么空间,所以可以继续操作。所以指针用完后要置NULL,否则仍然指向那片空间。\
如果空间被别的程序占用了,但是这个指针仍然指向他。如果这个指针被误操作使用了这片内存,就会破坏别的程序的 结构了。
return 0;