C++之复制构造函数

来源:互联网 发布:c语言null包含头文件 编辑:程序博客网 时间:2024/05/29 06:58

  //复制构造函数
  #include<iostream.h>
  #include<string.h>
  class string
  {
  private:
   char * str;
  public:
  
  string(char * s=" ")
  {
   cout << "在构造函数中/n";
   int size=strlen(s);
   str=new char[size+1];
   strcpy(str,s);
  }
  string(string & ss)//复制构造函数
  {
   cout << "在复制构造函数中/n";
   str=new char[strlen(ss.str)+1];
   strcpy(str,ss.str);
  }
  ~string()
  {delete str;}
  void showstring()
  {cout << str << endl;}
  };
  void main()
  {
   string s1="内存中得字符串";
   cout << "s1 = ";
   s1.showstring();
   string s2(s1);
   cout << "s2 = ";
   s2.showstring();
  }  
原创粉丝点击