自己实现c++中string 类
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 类的更多相关文章
- Java中String类的方法及说明
String : 字符串类型 一. String sc_sub = new String(c,3,2); // String sb_copy = new String(sb) ...
- JDK6与JDK7中String类subString()方法的区别
1.subString()方法的作用 subString(int beginIndex, int endIndex)方法的返回的是以beginIndex开始到 endIndex-1结束的某个调用字符串 ...
- ptypes中string类的空间分配
问题描述: 在学习ptypes中string类的空间分配时,经常使分配的空间超出实际所需的空间 使用的分配函数是:_alloc函数 注: 在_alloc函数中调用了 ...
- java中String类学习
java中String类的相关操作如下: (1)初始化:例如,String s = “abc”; (2)length:返回字符串的长度. (3)charAT:字符操作,按照索引值获得字符串中的指定字符 ...
- c++中string类的详解
,<时返回-1,==时返回0 string的子串:string substr(int pos = 0,int n = npos) const;//返回pos开始的n个字符组成的字符串strin ...
- 标准C++中string类的用法
转自博客园:http://www.cnblogs.com/xFreedom/archive/2011/05/16/2048037.html 相信使用过MFC编程的朋友对CString这个类的印象应该非 ...
- c++中string类的具体解释
通过在站点上的资料搜集,得到了非常多关于string类使用方法的文档,通过对这些资料的整理和增加一些自己的代码,就得出了一份比較完整的关于string类函数有哪些和如何用的文档了! 以下先罗列出str ...
- 关于Java中String类的hashCode方法
首先来看一下String中hashCode方法的实现源码 public int hashCode() { int h = hash; if (h == 0 && value.lengt ...
- 在java中String类为什么要设计成final
在java中String类为什么要设计成final? - 胖胖的回答 - 知乎 https://www.zhihu.com/question/31345592/answer/114126087
- 【转载】Java中String类的方法及说明
转载自:http://www.cnblogs.com/YSO1983/archive/2009/12/07/1618564.html String : 字符串类型 一. String sc_ ...
随机推荐
- 自定义PropertyGrid控件【转】
自定义PropertyGrid控件 这篇随笔其实是从别人博客上载录的.感觉很有价值,整理了一下放在了我自己的博客上,希望原作者不要介意. 可自定义PropertyGrid控件的属性.也可将属性名称显示 ...
- Windows 如何为绿色软件运行时添加参数 如最小化,无窗口运行
1 有些软件运行的时候需要或者可以添加参数来实现一些特殊要求,比如开机自启动,运行时不显示主界面,不显示托盘图标等,比如下面的这个流量精灵软件,"urlcore.exe /h /r /t 4 ...
- HDOJ题目2089 不要62(数位DP)
不要62 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- 绝不要在构造函数和析构过程中调用virtual函数
下面是一个用来塑模股市交易的类: derived的类的构造函数被调用,但是首先得调用基类Transaction的构造函数,但是在后面还得调用virrual函数,这个时候子类的对象的构造还没有完成,那么 ...
- git操作演示
阶段一: git init git config --global user.email "you@example.com" git config --global user.na ...
- byte数组和文件的相互转换
/** * 获得指定文件的byte数组 */ private byte[] getBytes(String filePath){ byte[] buffer = null; try { File fi ...
- php逻辑操作符中&和&&的异同
php有5种算术操作符(+ - * / %),6种赋值操作符(+= -= *= /= %= .=),8种比较操作符(=== < > <= >= != <> !==) ...
- 02 http协议之方法与状态码
一:HTTP请求信息和响应信息的格式 请求: ()请求行 ()请求头信息 ()请求主体信息(可以没有) () 头信息结束后和主体信息之间要空一行 请求行又分3部分 请求方法 请求路径 所用的协议 请求 ...
- CCBPM中的消息机制,CCIM服务端安装说明
1.改动LeeIMService.exe.config服务端配置文件: 2.client的配置就比較简单了,仅仅须要在"SercerIP"节点上写上server的IP: 3.然后要 ...
- IOS之禁用UIWebView的默认交互行为
本文转载至 http://my.oschina.net/hmj/blog/111344 UIKit提供UIWebView组件,允许开发者在App中嵌入Web页面.通过UIWebView组件 ...