博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
编写类String的构造函数、析构函数和赋值函数
阅读量:4225 次
发布时间:2019-05-26

本文共 780 字,大约阅读时间需要 2 分钟。

class CString{public:	CString(const char *str = NULL);	//普通构造函数	CString(const CString &other);		//拷贝构造函数	~CString();							//析构函数	CString &operator = (const CString &other); //赋值函数private:	char *m_data;		//用于保存字符串	};CString::CString(const char *str){	if (str==NULL)	{		m_data = new char[1];		*m_data = '\0';	}	else	{		int length = strlen(str);		m_data = new char[length+1];		strcpy(m_data,str);	}}CString::~CString(){	delete[] m_data;}CString::CString(const CString &other){	int lenght = strlen(other.m_data);	m_data = new char[lenght+1];	strcpy(m_data, other.m_data);}CString& CString::operator = (const CString &other){	if (this==&other)		return *this;	delete[] m_data;	int length = strlen(other.m_data);	m_data = new char[length+1];	strcpy(m_data, other.m_data);	return *this;}

转载地址:http://ubkqi.baihongyu.com/

你可能感兴趣的文章
nginx(三) nginx配置:反向代理 负载均衡 后端健康检查 缓存
查看>>
jQuery核心--多库共存
查看>>
6 51点亮第一个LED
查看>>
Multisim 14.0 搭建并仿真51单片机最小系统
查看>>
增加windows下Tomcat运行时的内存
查看>>
tomcat群集中session共享的几个方案
查看>>
查找google谷歌北京IP地址的方法
查看>>
java 核心技术Ⅱ--章六:时间与日期API
查看>>
链表,循环链表,双向链表,判环和入环点
查看>>
浅谈HashMap,HashTable,ConcurrentHashMap,WeakHashMap,HashMap源码分析
查看>>
云创大数据校企合作项目斩获“全国校企合作十佳案例”
查看>>
云创大数据与宽泛科技签订战略合作协议
查看>>
免费!免费!免费!全国高校大数据师资实战免费培训班
查看>>
“南京市独角兽瞪羚企业俱乐部创始人简餐会”走进云创
查看>>
本科大数据专业该怎么上?
查看>>
云创大数据1+X大数据应用部署与调优职业技能等级证书预申报正式开启!
查看>>
人工智能需要一个可被证明的理论作为基础 | 哈佛丘成桐
查看>>
入门 | 一文概览深度学习中的激活函数
查看>>
一分钟整明白Tensorflow Extended
查看>>
人工智能再次参加高考:和作家比写作文,AI能打多少分?
查看>>