从零开始学C++之运算符重载(三):完善String类([]、 +、 += 运算符重载)、和运算符重载...
在前面文章中使用过几次String类的例子,现在多重载几个运算符,更加完善一下,并且重载流类运算符。
[]运算符重载
+运算符重载
+=运算符重载
<<运算符重载
>>运算符重载
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 |
#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_ |
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 |
#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 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) |
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; return 0; |
需要注意的是,不能将String类的构造函数声明为explicit,否则 String s3 = "xxx"; 编译出错;operator[] 的non const 版本调用了const 版本的实现,其中使用了static_cast和 const_cast 两种类型转换操作符,可以参考这里;operator+ 调用了operator+= 的实现;只能将流类运算符重载为友元函数,因为第一个参数是流类引用,不是String 类。
转载于:https://www.cnblogs.com/jiangu66/p/3184705.html
从零开始学C++之运算符重载(三):完善String类([]、 +、 += 运算符重载)、和运算符重载...相关推荐
- 【高德地图API】从零开始学高德JS API(三)覆盖物——标注|折线|多边形|信息窗口|聚合marker|麻点图|图片覆盖物
原文地址为: [高德地图API]从零开始学高德JS API(三)覆盖物--标注|折线|多边形|信息窗口|聚合marker|麻点图|图片覆盖物 摘要:覆盖物,是一张地图的灵魂.有覆盖物的地图,才是完整的 ...
- python计算复数的辐角_Python 自定义类中的函数和运算符重载
如果你曾在字符串(str)对象上进行过 + 或 * 运算,你一定注意到它跟整数或浮点数对象的行为差异: >>> # 加法 >>> 1 + 2 3 >>& ...
- 92_特殊方法(魔术方法)和运算符重载
96.特殊方法(魔术方法)和运算符重载 python - 特殊方法查找-文档1 python - 特殊方法-文档2 Python的运算符实际上是通过调用对象的特殊方法实现的.比如: 测试代码1 a = ...
- 从零开始学 Web 之 ES6(三)ES6基础语法一
大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...
- 从零开始学 Web 之 JavaScript(三)函数
大家好,这里是「 Daotin的梦呓 」从零开始学 Web 系列教程.此文首发于「 Daotin的梦呓 」公众号,欢迎大家订阅关注.在这里我会从 Web 前端零基础开始,一步步学习 Web 相关的知识 ...
- 【从零开始学c++】4.char数组和string的千丝万缕
目录 1.char数组: char的赋值方法: char数组的赋值方法: ASCLL码: char类函数:需包含头文件 2.stirng: 3.对char数组的输入: 1.cin: 2.cin.etl ...
- python方法与重载_python特殊方法和运算符重载(番外--重载)
python的运算符实际上是通过对象的特殊方法实现的 [例如] a=20 b=30 c=a+b d=a.__add__(b) print("c=",c) print("d ...
- C++友元和运算符重载
C++友元 友元就是friend修饰函数或者类,友元只是单纯的提供一个场所赋予对象具有无视权限功能(不是说直接就可以访问另一个数据) 友元函数 友元函数不属于类,就是一个普通函数,类外实现不需要类名限 ...
- C++学习记录---(6)类和对象-----友元和运算符重载
1.友元 友元可以让一个函数或者其他类访问一个类中的私有成员. 主要分为,全局函数做友元.其他类做友元和成员函数做友元. (1)友元 在类中声明: friend 函数名(): (2)其他类做友元 在类 ...
最新文章
- Android中四种启动模式,最容易理解的小白教程
- 【解题报告】Leecode 807. 保持城市天际线——Leecode每日刷题系列
- MFC之实现鼠标自动左击,频率可调,支持热键
- 0.IDA的反汇编算法方式
- Cross-Site Scripting(XSS): 跨站脚本攻击介绍
- mysql日志文件转存_【转】Mysql日志文件
- jquery 固定导航
- 在Mac OS X中配置Apache + PHP + MySQL 很详细
- nrf52840蓝牙协议栈主机BLE串口
- 计算机cpu多大,电脑cpu多少容量
- 商业智能知识分享:BI的4大核心技术
- C语言实现:输出明天的日期
- access/sql server笔记(20160818)
- MT6737/MT6737T/MT6737M处理器功能介绍,MT6737芯片资料下载
- qgis面图层周长面积计算(视频)
- java科学型计算器代码_用JAVA编写的科学计算器源代码
- 数独游戏(回溯算法)
- 传统防火墙与Web应用程序防火墙(WAF)的区别
- 宝塔BT面板专业版(付费)和免费版有什么区别?
- 第五届蓝桥杯C/C++本科B组(真题试做)(1~5)
热门文章
- (转)mysql同步复制
- 计算机绘图员 机械 实训二,计算机绘图员[机械]实训形考.doc
- c语言冒泡排序_图文解析:如何用PLC梯形图实现冒泡排序算法?
- Unity3dRPG 相机跟随player旋转_跟随式灌装机
- php输出多行多列,数据库查询记录php 多行多列显示
- ajax前台转换json数据库,基于jQuery的ajax功能实现web service的json转化
- mysql中ip用什么存,在MySQL中,价钱和IP用什么类型存储最好?
- 【系统架构设计师】软考高级职称,一次通过,倾尽所有,2016年下半年系统架构设计师考试论文真题(论述软件设计模式技术及应用)
- update yum 到指定版本_CentOS 使用yum update 更新时保留特定版本的软件
- php的setinc方法,thinkphp3.2.0 setInc方法 源码全面解析