string类的实现
来源:互联网 发布:linux cp几个文件 编辑:程序博客网 时间:2024/06/13 15:03
#include<stdio.h>#include <iostream>#include<string.h>using namespace std;class string_my{public: string_my(const char *str=NULL);//普通构造函数 string_my(const string_my &other);//拷贝构造函数 ~ string_my(void);//析构函数 string_my& operator=(const string_my &other);//赋值函数 friend ostream & operator<<(ostream& o,const string_my &str);private: char *m_data;};ostream & operator<<(ostream &o,const string_my &str){ o<<str.m_data; return o;}string_my::string_my(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); }}string_my::~string_my(void){ delete []m_data;}string_my::string_my(const string_my &other){ int length=strlen(other.m_data); m_data=new char[length+1]; strcpy(m_data,other.m_data);}string_my& string_my::operator=(const string_my &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;}int main(){ string_my s("sasa"); cout<<s<<endl; string_my q("qqqqq"); s=q; cout<<s<<endl; s="12123343"; cout<<s<<endl;}
阅读全文
0 0
- string类的实现
- String类的实现
- String类的实现
- String类的实现
- String 类的实现
- String类的实现
- string类的实现
- String类的实现
- String类的实现
- string类的实现
- string类的实现
- string类的实现
- string类的实现
- string类的实现
- string类的实现
- String 类的实现
- String类的实现
- String类的实现
- 一分钟实现轮播图
- 自学JavaScript语法的收获
- Arrays.asList().add()异常
- String类(二)
- Filezilla server配置FTP服务器中的各种问题与解决方法
- string类的实现
- lua垃圾回收机制
- 数据结构线性表
- 如何从MongoDB迁移到MySQL?这有现成经验!
- 数据库设计三大范式
- 在支付宝大数据间谍面前,网络小透明?不存在的
- 史上最全的ubuntu16.04安装nvidia驱动+cuda9.0+cuDnn7.0
- 程序员的人生规划
- Entity Framework 复杂类型