C++中copy constructor

来源:互联网 发布:linux can总线编程 编辑:程序博客网 时间:2024/05/16 06:24
#include <iostream>#include <string>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())) {}    // 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;}
原创粉丝点击