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. Git以及github的使用方法(四),版本回退

    现在,你已经学会了修改文件,然后把修改提交到Git版本库,现在,再练习一次,修改readme.txt文件如下: Git is a distributed version control system. ...

  2. mt-datetime-picker type="date" 时间格式 bug

    1.mint-ui 中 mt-datetime-picker 组件,存在日期格式 bug <!-- 日期选择器 --> <template> <div class=&qu ...

  3. run kubernetes

    use kubeadm in lab only,,, do not use it in production      install docker 1.12.6 -..   please noted ...

  4. nagios插件之登陆SBC监控电话数

    运行:sbc_calls_status_new auto_ssh_sbc_10_17.sh | auto_ssh_sbc_11_17.sh vi sbc_calls_status_new.c #inc ...

  5. Visual Studio 12无法调试Silverligh应用程序的问题

    环境: Win7 Ultimate X64 Visual Studio 12 (以下简称 VS12) Internet Explorer 9(以下简称 IE9) Silverlight5_x64 ad ...

  6. Failure [INSTALL_FAILED_ALREADY_EXISTS]

    1.发生原因 做unity开发的同事说apk无法安装,要我帮忙看下,然后我通过命令adb install安装apk,出现此提示 2.解决过程 首先想到的仍然是安装包已存在的问题,结果使用清理类软件清理 ...

  7. Linux 下装mysql[Ubuntu & CentOS]

    Ubuntu: https://blog.csdn.net/hipilee/article/details/77610916

  8. HDU 5313 Bipartite Graph(二分图染色+01背包水过)

    Problem Description Soda has a bipartite graph with n vertices and m undirected edges. Now he wants ...

  9. Linux就该这么学--命令集合3(文本文件编辑命令)

    1.cat命令查看纯文本文件(较短):(cat [选项] [文件]) cat -n showpath.sh 附录: -n 显示行号 -b 显示行号(不包括空行) -A 显示出“不可见”的符号,如空格, ...

  10. wireshark 学习 2

    使用wireshark抓到的wifi数据包如果是加密的,就只能显示密文,无法得到真正的数据. 如果知道AP和SSID和key,就可以解密wifi数据包,显示上层协议的数据. 在wireshark中设置 ...