在前面文章中使用过几次String类的例子,现在多重载几个运算符,更加完善一下,并且重载流类运算符。

[]运算符重载

+运算符重载

+=运算符重载

<<运算符重载
>>运算符重载

String.h:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
 
#ifndef _STRING_H_
#define _STRING_H_
#include <iostream>
using namespace std;

class String
{
public:
    String(const char *str = "");
    String(const String &other);
    String &operator=(const String &other);
    String &operator=(const char *str);

bool operator!() const;
    char &operator[](unsigned int index);
    const char &operator[](unsigned int index) const;

friend String operator+(const String &s1, const String &s2);
    String &operator+=(const String &other);

friend ostream &operator<<(ostream &os, const String &str);
    friend istream &operator>>(istream &is, String &str);
    ~String(void);

void Display() const;
    int Length() const;
    bool IsEmpty() const;

private:
    String &Assign(const char *str);
    char *AllocAndCpy(const char *str);
    char *str_;
};

#endif // _STRING_H_

String.cpp:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
 
#pragma warning(disable:4996)
#include "String.h"
#include <string.h>
//#include <iostream>
//using namespace std;

String::String(const char *str)
{
    str_ = AllocAndCpy(str);
}

String::String(const String &other)
{
    str_ = AllocAndCpy(other.str_);
}

String &String::operator=(const String &other)
{
    if (this == &other)
        return *this;

return Assign(other.str_);
}

String &String::operator=(const char *str)
{
    return Assign(str);
}

String &String::Assign(const char *str)
{
    delete[] str_;
    str_ = AllocAndCpy(str);
    return *this;
}

bool String::operator!() const
{
    return strlen(str_) != 0;
}

char &String::operator[](unsigned int index)
{
    //return str_[index];
    //non const 版本调用 const版本

return const_cast<char &>(static_cast<const String &>(*this)[index]);
}

const char &String::operator[](unsigned int index) const
{
    return str_[index];
}

String::~String()
{
    delete[] str_;
}

char *String::AllocAndCpy(const char *str)
{
    int len = strlen(str) + 1;
    char *newstr = new char[len];
    memset(newstr, 0, len);
    strcpy(newstr, str);

return newstr;
}

void String::Display() const
{
    cout << str_ << endl;
}

int String::Length() const
{
    return strlen(str_);
}

bool String::IsEmpty() const
{
    return Length() == 0;
}

String operator+(const String &s1, const String &s2)
{
    //int len = strlen(s1.str_) + strlen(s2.str_) + 1;
    //char* newstr = new char[len];
    //memset(newstr, 0, len);
    //strcpy(newstr, s1.str_);
    //strcat(newstr, s2.str_);
    //
    //String tmp(newstr);
    //delete newstr;
    String str = s1;
    str += s2;
    return str;
}

String &String::operator+=(const String &other)
{
    int len = strlen(str_) + strlen(other.str_) + 1;
    char *newstr = new char[len];
    memset(newstr, 0, len);
    strcpy(newstr, str_);
    strcat(newstr, other.str_);

delete[] str_;

str_ = newstr;
    return *this;
}

ostream &operator<<(ostream &os, const String &str)
{
    os << str.str_;
    return os;
}

istream &operator>>(istream &is, String &str)
{
    char tmp[1024];
    cin >> tmp;
    str = tmp;
    return is;
}

main.cpp:

 C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 
#include "String.h"
#include <iostream>
using namespace std;

int main(void)
{
    String s1("abcdefg");

char ch = s1[2];
    cout << ch << endl;

s1[2] = 'A';
    s1.Display();

const String s2("xyzabc");
    ch = s2[2];
    //s2[2] = 'M'; Error
    s2.Display();

String s3 = "xxx";
    String s4 = "yyy";

String s5 = s3 + s4;
    s5.Display();

String s6 = "aaa" + s3 + "sdfadfa" + "xxxx";
    s6.Display();

s3 += s4;
    s3.Display();

cout << s3 << endl;

String s7;
    cin >> s7;
    cout << s7 << endl;

if (!s7.IsEmpty())
cout<<s7.Length()<<endl;

return 0;
}

需要注意的是,不能将String类的构造函数声明为explicit,否则"xxx";
编译出错;operator[] 的non const 版本调用了const 版本的实现,其中使用了static_cast和 const_cast 两种类型转换操作符,可以参考这里;operator+ 调用了operator+= 的实现;只能将流类运算符重载为友元函数,因为第一个参数是流类引用,不是String 类。

通过实现这样一个字符串类,我们可以熟悉基本的内存管理与拷贝控制。

参考:

C++ primer 第四版
Effective C++ 3rd
C++编程规范

完善String类([]、 +、 += 运算符重载)、>>和<<运算符重载的更多相关文章

  1. 从零开始学C++之运算符重载(三):完善String类([]、 +、 += 运算符重载)、>>和<<运算符重载

    在前面文章中使用过几次String类的例子,现在多重载几个运算符,更加完善一下,并且重载流类运算符. []运算符重载 +运算符重载 +=运算符重载 <<运算符重载 >>运算符重 ...

  2. C++入门经典-例7.10-运算符的重载,重载加号运算符

    1:曾经介绍过string类型的数据,它是C++标准模版库提供的一个类.string类支持使用加号“+”连接两个string对象.但是使用两个string对象相减确实非法的,其中的原理就是C++所提供 ...

  3. String 类实现 以及>> <<流插入/流提取运算符重载

    简单版的String类,旨在说明>> <<重载 #include <iostream> //#include <cstring>//包含char*的字符 ...

  4. string类中运算符重载实现

    C++中预定义的加.减等运算符的操作对象只能是基本的数据类型.如果要在用户自定义的类型对象上应用同样的运算符,就需要通过运算符重载来重新定义其实现,使它能够用于自定义类型执行特定的操作,所以运算符重载 ...

  5. C++重载运算符练习--对people类重载“= =”运算符和“=”运算符

    题目描述 对people类重载“= =”运算符和“=”运算符,“==”运算符判断两个people类对象的id属性是否相等:“=”运算符实现people类对象的赋值操作. 代码如下 #include&l ...

  6. C++编写字符串类CNString,该类有默认构造函数、类的拷贝函数、类的析构函数及运算符重载

    编码实现字符串类CNString,该类有默认构造函数.类的拷贝函数.类的析构函数及运算符重载,需实现以下“=”运算符.“+”运算.“[]”运算符.“<”运算符及“>”运算符及“==”运算符 ...

  7. C++ //多态 //静态多态:函数重载 和 运算符重载 属于静态多态 ,复用函数名 //动态多态:派生类和虚函数实现运行时多态

    1 //多态 2 //静态多态:函数重载 和 运算符重载 属于静态多态 ,复用函数名 3 //动态多态:派生类和虚函数实现运行时多态 4 5 //静态多态和动态多态的区别 6 //静态多态的函数地址早 ...

  8. C++重载流运算符,将存储结构体的vector直接写入文件

    我们知道,当vector很大的时候,如果使用循环的方式将其中的元素写入文件将非常费时,因此有没有办法将vector一次性写入文件呢? 采用流运算符重载的方法可以做到,不仅基本类型的vector可以一次 ...

  9. [置顶] operator overloading(操作符重载,运算符重载)运算符重载,浅拷贝(logical copy) ,vs, 深拷贝(physical copy)

    operator overloading(操作符重载,运算符重载) 所谓重载就是重新赋予新的意义,之前我们已经学过函数重载,函数重载的要求是函数名相同,函数的参数列表不同(个数或者参数类型).操作符重 ...

随机推荐

  1. C#中有关资源、BeginInvoke, Invoke和事件的事情

    事情是这么来的,我开发的一个程序报了一个错误 “在创建窗口句柄之前,不能在控件上调用 Invoke 或 BeginInvoke错误”. 然后我在网上查资料,发现一个有意思的问题,文章出处为“在创建窗口 ...

  2. delphi 搭建安卓开发环境

    delphi 搭建安卓开发环境 DELPHI安装成功以后,怀着激动的心情,使用IDE向导生成安卓DEMO程序,BUILD,想马上看到编译成功的提示,结果报错,不由得傻眼了.DELPHI怎么这么差? 原 ...

  3. sessionid与cookie

    转自:http://smiky.iteye.com/blog/649164 发现自己真的是很笨,过去一直用jsp,从来不用怕心用户信息放在session里面会找不到,现在不用jsp,前台全用html, ...

  4. CentOS安装mysql*.rpm提示conflicts with file from package的解决的方法

    CentOS 6.5下安装MySql 5.6 解压文件:tar xvf MySQL-5.6.19-1.linux_glibc2.5.x86_64.rpm-bundle.tar 释放出下面文件: MyS ...

  5. POJ 1740:A New Stone Game

    A New Stone Game Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5113 Accepted: 2806 Desc ...

  6. Logback的配置和使用(终极)

    1.logback介绍 logback是由log4j创始人设计的又一个开源日志组件.logback当前分成三个模块:logback-core,logback-classic和logback-acces ...

  7. iOS中WebKit框架应用与解析

    WebKit是iOS8之后引入的专门负责处理网页视图的框架,其比UIWebView更加强大,性能也更优. 引言 在iOS8之前,在应用中嵌入网页通常需要使用UIWebView这样一个类,这个类通过UR ...

  8. ShellCode的编写入门

    上次学习了下堆喷漏洞的原理,虽说之前有学习过缓冲区溢出的原理,但还没了解过堆喷这个概念,于是趁此机会学习了,顺便复习了缓冲区溢出这块知识,之前由于各种原因对Shellcode的编写只是了解个大概,并没 ...

  9. java页面url传值中文编码&解码

    URL参数中有中文值,传到服务端,在用request.getParameter()方法,得到的常常会是乱码. 这将涉及到字符解码操作,我们在应用中常常会用new String(fieldType.ge ...

  10. Android -- 写xml到SD卡中

    信息类                                                                                          private ...