赋值构造函数和拷贝构造函数

来源:互联网 发布:软件开发企业 深圳 编辑:程序博客网 时间:2024/04/29 19:26
#include "stdafx.h" #include <string>#include <iostream>using namespace std;class String { char * m_data;public: String (const char * other) { cout<<"我是默认构造函数"<<endl;int len;len=strlen(other)+1;m_data=new char[len];strcpy_s(m_data,len,other);}String (const String& other) { cout<<"我是拷贝构造函数"<<endl;int len;len=strlen(other.m_data)+1;m_data=new char[len];strcpy_s(m_data,len,other.m_data);}String & operator = (const String& other) { cout<<"我是赋值构造函数"<<endl;int len;len=strlen(other.m_data)+1;m_data=new char[len];strcpy_s(m_data,len,other.m_data);return *this;    } };  int main (void) { String a="qwe";String b("sss");String c=a;b=a;int aa;cin>>aa;} 


原创粉丝点击