不解Ms-VC++6 下的 std::auto_prt 之怪现象?请教大虾了...
//不解Ms-VC++6 下的 std::auto_prt 之怪现象?请教大虾了...
#include <iostream>
#include <memory>
class A
{
public:
A():_i(10)
{
std::cout<<"A::A()"<<std::endl;
}
~A()
{
std::cout<<"A::~A()"<<std::endl;
}
public:
int _i;
};
void main(void)
{
std::cout<<"Hello,the world!"<<std::endl;
std::auto_ptr<A> p1(new A);//A::A()
std::cout<<"p1=> p1->_i = "<<p1->_i<<std::endl;
std::auto_ptr<A> p2(new A);//A::A()
p2->_i = 20;
std::cout<<"p2=> p2->_i = "<<p2->_i<<std::endl;
std::cout<<"p2=p1 -> begin"<<std::endl;
p2 = p1;//A::~A()
p1->_i = 8;
std::cout<<"p2=p1 -> end"<<std::endl;
std::cout<<"p2=> p2->_i = "<<p2->_i<<std::endl;
std::cout<<"p1=> p1->_i = "<<p1->_i<<std::endl;
{
std::cout<<" Test p3 -> Begin "<<std::endl;
std::auto_ptr<A> p3 = p2;
std::cout<<"p3 = p2 -> end"<<std::endl;
std::cout<<"p1=> p1->_i = "<<p1->_i<<std::endl;
std::cout<<"p2=> p2->_i = "<<p2->_i<<std::endl;
std::cout<<"p3=> p3->_i = "<<p3->_i<<std::endl;
std::cout<<" Test p3 -> end "<<std::endl;
}//A::~A()
//在这之后为什么不出错?请教大虾
p1->_i = 100;
std::cout<<"p1->_i = 100"<<std::endl;
std::cout<<"p2=> p2->_i = "<<p2->_i<<std::endl;
std::cout<<"p1=> p1->_i = "<<p1->_i<<std::endl;
std::cout<<"The end!"<<std::endl;
}
/////////////////////////////////
//ms vc6 下运行结果
/*
Hello,the world!
A::A()
p1=> p1->_i = 10
A::A()
p2=> p2->_i = 20
p2=p1 -> begin
A::~A()
p2=p1 -> end
p2=> p2->_i = 8
p1=> p1->_i = 8
Test p3 -> Begin
p3 = p2 -> end
p1=> p1->_i = 8
p2=> p2->_i = 8
p3=> p3->_i = 8
Test p3 -> end
A::~A()//在这一步之前的结果都好理解,不解的是之后为什么不出错?
p1->_i = 100
p2=> p2->_i = 100
p1=> p1->_i = 100
The end!
Press any key to continue
*/