Demo1:
#include <iostream>
#include <string>
#include <memory>
using std::cout;
using std::string;
class A
{
public:
int number;
string info;
A(int param=0)
{
number=param;
cout<<"A() :"<<number<<"\n";
}
~A()
{
cout<<"~A() :"<<number<<"\n";
}
void print() const
{
cout<<"print() "<<info.c_str()<<std::endl;
}
};
void test();
int main()
{
test();
A(1);
return 0;
}
void test()
{
std::auto_ptr<A> _ptr(new A(1));
if(_ptr.get())
{
_ptr->print();
_ptr.get()->info="Addition";
_ptr->print();
(*_ptr).info+="other";
_ptr->print();
}
}
測試結果