c++智能指針學習

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();
    }
}
測試結果

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章