从零开始学C++之运算符重载(三):完善String类([]、 +、 += 运算符重载)、>>和<<运算符重载
来源:互联网 发布:win7网络连接显示红叉 编辑:程序博客网 时间:2024/06/03 14:06
在前面文章中使用过几次String类的例子,现在多重载几个运算符,更加完善一下,并且重载流类运算符。
[]运算符重载+运算符重载+=运算符重载<<运算符重载>>运算符重载
String.h:
#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:
#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:
#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,否则 String s3 = “xxx”; 编译出错;operator[] 的non const 版本调用了const 版本的实现,其中使用了static_cast和 const_cast 两种类型转换操作符,可以参考这里;operator+ 调用了operator+= 的实现;只能将流类运算符重载为友元函数,因为第一个参数是流类引用,不是String 类。
通过实现这样一个字符串类,我们可以熟悉基本的内存管理与拷贝控制。
参考:
C++ primer 第四版
Effective C++ 3rd
C++编程规范
转载自http://blog.csdn.net/jnu_simba/article/details/9294115
0 0
- 从零开始学C++之运算符重载(三):完善String类([]、 +、 += 运算符重载)、>>和<<运算符重载
- 从零开始学C++之运算符重载(三):完善String类([]、 +、 += 运算符重载)、>>和<<运算符重载
- 从零开始学C++之运算符重载(三):完善String类([]、 +、 += 运算符重载)、>>和<<运算符重载
- 从零开始学C++之运算符重载(二):++运算符重载、!运算符重载、赋值运算符重载
- 从零开始学C++之运算符重载(二):++运算符重载、!运算符重载、赋值运算符重载
- 从零开始学C++之运算符重载(二):++运算符重载、!运算符重载、赋值运算符重载
- 从零开始学C++之运算符重载(四):类型转换运算符、*运算符重载、->运算符重载、operator new 和 operator delete
- 从零开始学C++之运算符重载(四):类型转换运算符、*运算符重载、->运算符重载、operator new 和 operator delete .
- 从零开始学C++之运算符重载(四):类型转换运算符、*运算符重载、->运算符重载、operator new 和 operator delete
- 十九、运算符重载(三)String类的改进实现、[]运算符重载、+运算符重载、+=运算符重载、<<运算符重载、>>运算符重载
- 从零开始学C++之运算符重载(一):以成员函数方式重载、以友元函数方式重载
- 从零开始学C++之运算符重载(一):以成员函数方式重载、以友元函数方式重载
- 从零开始学C++之运算符重载(一):以成员函数方式重载、以友元函数方式重载
- 从零开始学C++之运算符重载(一):以成员函数方式重载、以友元函数方式重载
- String运算符重载
- 运算符重载(c++)
- 重载之运算符重载
- 【C++】重载运算符和重载函数
- Spark性能优化:数据倾斜调优
- Fedora 24安装Virtual box增强功能包
- grep命令
- 采集某一个数据时间段的访问量比率
- 等价类划分的原则
- 从零开始学C++之运算符重载(三):完善String类([]、 +、 += 运算符重载)、>>和<<运算符重载
- WindowManagerService和应用程序的IPC过程
- Fibonacci数列(矩阵乘法快速幂)
- Java线程中的join和yield
- leetcode_107. Binary Tree Level Order Traversal II 二叉树层次遍历,利用python的list特性实现队列功能
- Spark性能优化:shuffle调优
- 如何用IDEA一步一步开发WebService客户端程序
- 工厂模式
- Android_获取手机版本型号