完善String类([]、 +、 += 运算符重载)、>>和<<运算符重载
在前面文章中使用过几次String类的例子,现在多重载几个运算符,更加完善一下,并且重载流类运算符。
[]运算符重载
+运算符重载
+=运算符重载
<<运算符重载
>>运算符重载
String.h:
|
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 bool operator!() const; friend String operator+(const String &s1, const String &s2); friend ostream &operator<<(ostream &os, const String &str); void Display() const; private: #endif // _STRING_H_ |
String.cpp:
|
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) String::String(const String &other) String &String::operator=(const String &other) return Assign(other.str_); String &String::operator=(const char *str) String &String::Assign(const char *str) bool String::operator!() const char &String::operator[](unsigned int index) return const_cast<char &>(static_cast<const String &>(*this)[index]); const char &String::operator[](unsigned int index) const String::~String() char *String::AllocAndCpy(const char *str) return newstr; void String::Display() const int String::Length() const bool String::IsEmpty() const String operator+(const String &s1, const String &s2) String &String::operator+=(const String &other) delete[] str_; str_ = newstr; ostream &operator<<(ostream &os, const String &str) istream &operator>>(istream &is, String &str) |
main.cpp:
|
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) char ch = s1[2]; s1[2] = 'A'; const String s2("xyzabc"); String s3 = "xxx"; String s5 = s3 + s4; String s6 = "aaa" + s3 + "sdfadfa" + "xxxx"; s3 += s4; cout << s3 << endl; String s7; 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类([]、 +、 += 运算符重载)、>>和<<运算符重载的更多相关文章
- 从零开始学C++之运算符重载(三):完善String类([]、 +、 += 运算符重载)、>>和<<运算符重载
在前面文章中使用过几次String类的例子,现在多重载几个运算符,更加完善一下,并且重载流类运算符. []运算符重载 +运算符重载 +=运算符重载 <<运算符重载 >>运算符重 ...
- C++入门经典-例7.10-运算符的重载,重载加号运算符
1:曾经介绍过string类型的数据,它是C++标准模版库提供的一个类.string类支持使用加号“+”连接两个string对象.但是使用两个string对象相减确实非法的,其中的原理就是C++所提供 ...
- String 类实现 以及>> <<流插入/流提取运算符重载
简单版的String类,旨在说明>> <<重载 #include <iostream> //#include <cstring>//包含char*的字符 ...
- string类中运算符重载实现
C++中预定义的加.减等运算符的操作对象只能是基本的数据类型.如果要在用户自定义的类型对象上应用同样的运算符,就需要通过运算符重载来重新定义其实现,使它能够用于自定义类型执行特定的操作,所以运算符重载 ...
- C++重载运算符练习--对people类重载“= =”运算符和“=”运算符
题目描述 对people类重载“= =”运算符和“=”运算符,“==”运算符判断两个people类对象的id属性是否相等:“=”运算符实现people类对象的赋值操作. 代码如下 #include&l ...
- C++编写字符串类CNString,该类有默认构造函数、类的拷贝函数、类的析构函数及运算符重载
编码实现字符串类CNString,该类有默认构造函数.类的拷贝函数.类的析构函数及运算符重载,需实现以下“=”运算符.“+”运算.“[]”运算符.“<”运算符及“>”运算符及“==”运算符 ...
- C++ //多态 //静态多态:函数重载 和 运算符重载 属于静态多态 ,复用函数名 //动态多态:派生类和虚函数实现运行时多态
1 //多态 2 //静态多态:函数重载 和 运算符重载 属于静态多态 ,复用函数名 3 //动态多态:派生类和虚函数实现运行时多态 4 5 //静态多态和动态多态的区别 6 //静态多态的函数地址早 ...
- C++重载流运算符,将存储结构体的vector直接写入文件
我们知道,当vector很大的时候,如果使用循环的方式将其中的元素写入文件将非常费时,因此有没有办法将vector一次性写入文件呢? 采用流运算符重载的方法可以做到,不仅基本类型的vector可以一次 ...
- [置顶] operator overloading(操作符重载,运算符重载)运算符重载,浅拷贝(logical copy) ,vs, 深拷贝(physical copy)
operator overloading(操作符重载,运算符重载) 所谓重载就是重新赋予新的意义,之前我们已经学过函数重载,函数重载的要求是函数名相同,函数的参数列表不同(个数或者参数类型).操作符重 ...
随机推荐
- Add margining capability to a dc/dc converter
You can easily add margining capability—that is, the ability to digitally adjust the output voltage— ...
- How to open a web site with the default web browser in a NEW window
http://delphi.about.com/cs/adptips2004/a/bltip0504_4.htm When using ShellExecute (as explained in th ...
- Android SDK Manager 代理服务器设置
http://blog.csdn.net/star_splendid/article/details/6939063 自己机子更新的话,速度1KB/s 实在是等不及了~找方法吧 http://www. ...
- OSChina.net 的 Tomcat 配置 server.xml 参考
这是目前 oschina.net 正在使用的 tomcat 的 server.xml 的配置文件内容 <Server port="9005" shutdown="S ...
- Oracle简单脚本演示样例
Oracle简单脚本演示样例 1.添加表 --改动日期:2014.09.21 --改动人:易小群 --改动内容:新增採购支付情况表 DECLARE VC_STR VARCHAR2( ...
- nginx实现openfire负载均衡
Nginx版本必须是1.9以上,不然不支持tcp连接,要么加入其他插件 在nginx.conf文件中加入下图中的代码,图中的ip与端口配成自己的,配置好后,客户端请求listen监听的端口,ip为ng ...
- Mac OSX 系统目录结构(转)
摘要 在OS X的系统中,不再有Windows用户熟悉的C盘.D盘,这是因为OS X底层是Unix系统,其目录机构符合Unix系统的规范.MAC机器主板使用了Intel主导的EFI标准,硬盘分区格式采 ...
- ExtJS遮罩层Ext.loadMask
一.可以直接应用在元素上,如: var loadMarsk = new Ext.LoadMask(target, { msg:'正在处理数据,请稍候......', removeMask:true / ...
- Django的restful api三方包:djangorestframework
文档位置:https://www.django-rest-framework.org/ 代码位置:https://github.com/encode/django-rest-framework/tre ...
- [Todo]对于thrift和protobuf比较好的描述
比较跨语言通讯框架:thrift和Protobuf 全部thrift protobuf 前两天想在微博上发表一个观点:在现在的技术体系中,能用于描述通讯协议的方式很多,xml,json,protobu ...