博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
String类成员函数的实现
阅读量:7023 次
发布时间:2019-06-28

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

已知String类定义如下:

class String{public:    String(const char *str = NULL); // 通用构造函数    String(const String &another); // 拷贝构造函数    ~ String(); // 析构函数    String & operater =(const String &rhs); // 赋值函数private:    char *m_data; // 用于保存字符串};

尝试写出类的成员函数实现。

String::String(const char *str){   if ( str == NULL ) //strlen在参数为NULL时会抛异常才会有这步判断     {       m_data = new char[1] ;       m_data[0] = '\0' ;     }   else    {       m_data = new char[strlen(str) + 1];       strcpy(m_data,str);    }}String::String(const String &another){    m_data = new char[strlen(another.m_data) + 1];    strcpy(m_data,other.m_data);}String& String::operator =(const String &rhs){    if ( this == &rhs)        return *this ;    delete []m_data; //删除原来的数据,新开一块内存    m_data = new char[strlen(rhs.m_data) + 1];    strcpy(m_data,rhs.m_data);    return *this ;}String::~String(){    delete []m_data ;}

 

 

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

你可能感兴趣的文章
Oracle 数据库查看client的用户登录信息包括ip
查看>>
cacti脚本数据采集
查看>>
centos 下 django + uwsgi + nginx 快速搭建
查看>>
Kernel Trace System
查看>>
linux文件系统详解
查看>>
Vim入门基础
查看>>
我的友情链接
查看>>
ibatis mybatis sql语句配置 符号不兼容 大于号 小于号
查看>>
Alipay 开源 SofaRPC
查看>>
jquery的extend与fn.extend
查看>>
自动化测试应该学什么
查看>>
语音识别对比分析
查看>>
Linux命令之 wc
查看>>
在virtualbox中安装的ubuntu系统 共享数据空间分配的数据
查看>>
WinRAR4.11激活
查看>>
Oracle常用单行函数
查看>>
虚拟机客户端安装操作系统
查看>>
(fork)在局域网中部署微信服务器 (PHP)
查看>>
如何做好SEO
查看>>
Linux命令之free
查看>>