您的位置:首页 > 编程语言 > C语言/C++

c++ 构造函数深层复制示例

2011-04-05 20:14 399 查看
/*******************************************************
 *author:彭晓林
 *copyright: 版权所有,翻版不究
 *function: 构造函数深层复制示例
 ******************************************************/

#include <iostream>
#include <string>

using namespace std;

class DEMO

 public:
  DEMO(int a)
  {
   int* temp = new int;
   x = temp;

   *x = a;
  }
  void set(int a)
  {
   *x = a;
  }
  DEMO(DEMO &demo)
  {
   int* a = new int;

   *a = *(demo.x);
   *x = *a;
  }
 void print();

 private:
  int* x;
};

void DEMO::print()
{
 cout<<"x = "<<*x<<endl;
 cout<<"x address :"<<x<<endl;
}

int main()
{
 DEMO* TestA = new DEMO(1); 

 TestA->print();

 DEMO TestB(*TestA);

 TestB.print();

 TestB.set(32);

 TestA->print();

 TestB.print();

 while(1);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c++ function
相关文章推荐