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];// 得分点:对空字符串自动申请存放结束标志'\0'的,加分点:对m_data加NULL判断
*m_data = '\0';
}
else
{
int length = strlen(str);
m_data = new char[length + 1];// 若能加 NULL 判断则更好
strcpy(m_data, str);
}
} // String的析构函数
String::~String(void)
{
delete[] m_data; // 或delete m_data;
} //拷贝构造函数
String::String(const String &other)// 得分点:输入参数为const型
{
int length = strlen(other.m_data);
m_data = new char[length + 1];// 若能加 NULL 判断则更好
strcpy(m_data, other.m_data);
} //赋值函数
String & String::operator = (const String &other) // 得分点:输入参数为const型
{
if (this == &other)//得分点:检查自赋值
return *this;
if (m_data)
delete[] m_data;//得分点:释放原有的内存资源
int length = strlen(other.m_data);
m_data = new char[length + 1];//加分点:对m_data加NULL判断
strcpy(m_data, other.m_data);
return *this;//得分点:返回本对象的引用
}

  

高级版参考:https://blog.csdn.net/u010700335/article/details/40979037

自己实现c++中string 类的更多相关文章

  1. Java中String类的方法及说明

    String : 字符串类型 一.      String sc_sub = new String(c,3,2);    //      String sb_copy = new String(sb) ...

  2. JDK6与JDK7中String类subString()方法的区别

    1.subString()方法的作用 subString(int beginIndex, int endIndex)方法的返回的是以beginIndex开始到 endIndex-1结束的某个调用字符串 ...

  3. ptypes中string类的空间分配

    问题描述:            在学习ptypes中string类的空间分配时,经常使分配的空间超出实际所需的空间 使用的分配函数是:_alloc函数 注:        在_alloc函数中调用了 ...

  4. java中String类学习

    java中String类的相关操作如下: (1)初始化:例如,String s = “abc”; (2)length:返回字符串的长度. (3)charAT:字符操作,按照索引值获得字符串中的指定字符 ...

  5. c++中string类的详解

    ,<时返回-1,==时返回0  string的子串:string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串strin ...

  6. 标准C++中string类的用法

    转自博客园:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 相信使用过MFC编程的朋友对CString这个类的印象应该非 ...

  7. c++中string类的具体解释

    通过在站点上的资料搜集,得到了非常多关于string类使用方法的文档,通过对这些资料的整理和增加一些自己的代码,就得出了一份比較完整的关于string类函数有哪些和如何用的文档了! 以下先罗列出str ...

  8. 关于Java中String类的hashCode方法

    首先来看一下String中hashCode方法的实现源码 public int hashCode() { int h = hash; if (h == 0 && value.lengt ...

  9. 在java中String类为什么要设计成final

    在java中String类为什么要设计成final? - 胖胖的回答 - 知乎 https://www.zhihu.com/question/31345592/answer/114126087

  10. 【转载】Java中String类的方法及说明

    转载自:http://www.cnblogs.com/YSO1983/archive/2009/12/07/1618564.html String : 字符串类型 一.      String sc_ ...

随机推荐

  1. angular - 介绍

    导入全局样式,生产环境和浏览器环境都导入了. 不知否你还记得index.html那个里面的节点 熟悉吗? 很熟悉吧

  2. 判断是否是iso8859-1编码

    if (null == keyword || keyword.equals("关键字")) keyword = "";            if(keywor ...

  3. JavaWeb页面添加隐藏版权信息

    JavaWeb页面添加隐藏版权信息. 首先,我推荐一个值得玩味的版权站点,有兴趣的朋友能够去看上一看.Nazo Level 1,这个demo中我能发掘到有5个步骤,你看你能发现几层? 接下来.我来介绍 ...

  4. 不使用while,for,if等实现加法

    不使用if, while,for,switch等实现从1到10的加法 解:这里使用静态函数和静态变量实现,利用类似的方法也能够实现从1打印到1000 class TheSum{ public: The ...

  5. Activiti实战

    说实话,接触Activiti已经是3年前的事情,那时候组里想做一个流程自动化的application,并且记录用户点击.做单量等.第一次听说Activiti,感觉挺好奇的,遂看了下相关的文档跟同事的代 ...

  6. 按照HashMap中value值进行排序

    import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; ...

  7. duplicate symbols for architeture arm64 linker command failed with code 1(use-c to see invocation)

    duplicate symbols for architeture arm64  linker command failed with code 1(use-c to see invocation) ...

  8. windows 打开文件夹

    @echo off rem 建立链接 net use \\192.168.2.3\share /user:username password rem 打开共享文件夹 explorer \\192.16 ...

  9. EasyPlayer实现视频播放局部缩放、广角平移功能(类似水滴直播,快手视频)

    本文转自:http://blog.csdn.net/jyt0551/article/details/56063869 视频播放局部缩放.广角平移功能 在预览图片的时候,利用手势控制图片的缩放.平移,已 ...

  10. java之HashMap的遍历Iterator

    package com.ql_2;/* * 功能:HashMap 的使用 */import java.util.*; public class Test_2 { public static void ...