C++ 构造函数、拷贝构造函数、赋值构造函数、析构函数定义
来源:互联网 发布:淘宝开店要不要钱 编辑:程序博客网 时间:2024/06/15 07:19
Evelyn
1809335179@qq.com
构造函数、拷贝构造函数、赋值构造函数、析构函数定义
类string为例:
class string{public:string(const char *str = NULL);//普通构造函数string(const string &other);//拷贝构造函数~string(void);//析构函数string & operator = (const String &other);//赋值函数private:char *m_data;//用来保存字符串};
string::string(const char*str){if (str == NULL) { // 此处的判断m_data = new char[1];*m_data = '\0';}else {int len = strlen(str);m_data = new char[len + 1];strcpy(m_data,str);}}string::string(const string &other){int len = strlen(other.m_data);m_data = new char[len + 1];strcpy(m_data, other.m_data);}string& string:: operator=(const string &other) //{if (this==&other) //此处写法应注意 &return *this;if (m_data != NULL)delete[] m_data;int len = strlen(other.m_data);m_data = new char[len + 1];strcpy(m_data, other.m_data);return *this; //此处写法应注意 返回本引用,不要忘记}string::~string(){if (m_data != NULL)delete[] m_data;}
string a;
string b=a; //调用的是拷贝构造函数
string a;
string b;
b=a;//调用赋值构造函数
0 0
- C++ 构造函数、拷贝构造函数、赋值构造函数、析构函数定义
- 构造函数,拷贝构造函数,赋值函数,析构函数
- 构造函数,拷贝构造函数,赋值函数,析构函数
- 构造函数、析构函数、拷贝构造函数、赋值函数
- 构造函数 拷贝函数 赋值构造函数
- 构造函数 拷贝函数 赋值构造函数
- 构造函数、拷贝构造函数、赋值函数
- 【C++】拷贝构造函数和赋值函数
- 赋值函数、拷贝构造函数
- 拷贝构造函数赋值函数
- 拷贝构造函数,赋值函数
- 拷贝构造函数和赋值构造函数
- 拷贝构造函数与赋值构造函数
- 拷贝构造函数与赋值构造函数
- C++ 拷贝构造函数 赋值构造函数
- 拷贝构造函数与赋值构造函数
- 拷贝构造函数和赋值构造函数
- 拷贝构造函数 赋值构造函数
- DirectX SDK (June 2010)安装失败S1023
- 正则表达式大全(整理版)
- 数据结构实验之排序五:归并求逆序数
- iOS开发77-关于Build Active Architecture Only属性的配置
- 使用AVAudioRecorder录制声音(Swift)
- C++ 构造函数、拷贝构造函数、赋值构造函数、析构函数定义
- 微信,朋友圈,QQ,QQZone,微博分享的注意事项
- 转载一篇activity文章
- android之OkHttpClient通信
- 华为OJ——201301 JAVA 题目2-3级(棋盘走法)
- Log4net使用
- 多线程 定时器 Timer
- Android属性:所设属性值为何在重起后被清除
- 数据结构实验之排序五:归并求逆序数