菜菜

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
#include <iostream>
using namespace std;

class Example5
{
        string* ptr;
    public:
        Example5(const string& str):ptr(new string(str)){}
        ~Example5(){delete ptr;}
        //copy constructor
        Example5(const Example5& x):ptr(new string(x.content())){
            cout<<"copy constructor"<<endl;
        }
        //access content;
        const string& content()const{return *ptr;}
};

int main()
{
    Example5 foo("Example");
    Example5 bar = foo;
    cout<<"bar's content:"<<bar.content()<<'\n';
    return 0;
}

 

posted on 2018-02-15 22:08  好吧,就是菜菜  阅读(142)  评论(0编辑  收藏  举报