重载运算符 关系,下标,递增减,成员访问的重载

为了演示关系,下标,递增减,成员访问的重载,创建了下面2个类。

1,类StrBlob重载了关系,下标运算符

2,类StrBlobPtr重载了递增,抵减,成员访问运算符

1,类StrBlob功能概要:类型与vector,但只能存放string类型的数据。

2,类StrBlobPtr功能概要:类型指针,指向类StrBlob中的某个元素。

注意点:

1,->的重载方法的返回值必须是指针。

2,系统无法区分是前置的递增还是后置的,为了区分,在重载后置的时候,加一个int类型的参数,就告诉编译器这个是后置的递增。

3,后置的递增或者抵减的重载方法的返回值必须是值,不能是引用或者指针。因为返回的是值类型,所以会在retern处调用拷贝构造函数。前置的是放回引用,所以就不会调用拷贝构造函数。所以,能调用前置的时候,就调用前置的

StrBlob.h

#ifndef __STRBLOB_H__
#define __STRBLOB_H__ #include <memory>
#include <string>
#include <vector> class StrBlobPtr;
class StrBlob{
friend class StrBlobPtr;
friend bool operator==(const StrBlob&, const StrBlob&);
friend bool operator!=(const StrBlob&, const StrBlob&);
public:
typedef std::vector<std::string>::size_type size_type;
StrBlob();
StrBlob(std::initializer_list<std::string>);
size_type size() const{return data->size();}
bool empty()const {return data->empty();}
void push_back(const std::string& t){data->push_back(t);}
void pop_back();
std::string& front();
std::string& back(); std::string& operator[](size_type);
const std::string& operator[](size_type)const; StrBlobPtr begin();
StrBlobPtr end(); private:
std::shared_ptr<std::vector<std::string>> data;
void check(size_type, const std::string&) const;
};
bool operator==(const StrBlob&, const StrBlob&);
bool operator!=(const StrBlob&, const StrBlob&); #endif

github

StrBlob.cpp

#include "StrBlob.h"
//#include <iostream>
#include "StrBlobPtr.h" StrBlob::StrBlob() : data(std::make_shared<std::vector<std::string>>()){}
StrBlob::StrBlob(std::initializer_list<std::string> il) :
data(std::make_shared<std::vector<std::string>>(il)){} void StrBlob::check(size_type i, const std::string& msg)const{
if(i >= data->size()){
throw std::out_of_range(msg);
}
} std::string& StrBlob::front(){
check(0, "front");
return data->front();
} std::string& StrBlob::back(){
check(0, "back");
return data->back();
} void StrBlob::pop_back(){
check(0, "pop_back");
data->pop_back();
}
bool operator==(const StrBlob& lhs, const StrBlob& rhs){
/*
if(lhs.data->size() >=0 && lhs.data->size() == rhs.data->size()){
for(int i = 0; i < lhs.data->size(); ++i){
if((*lhs.data)[i] != (*rhs.data)[i]){
return false;
}
}
return true;
}
else{
return false;
}
*/
return *lhs.data == *rhs.data; }
bool operator!=(const StrBlob& lhs, const StrBlob& rhs){
return !operator==(lhs, rhs);
} std::string& StrBlob::operator[](size_type idx){
return (*data)[idx];
}
const std::string& StrBlob::operator[](size_type idx)const{
return (*data)[idx];
} StrBlobPtr StrBlob::begin(){
auto b = StrBlobPtr(*this);
return b;
}
StrBlobPtr StrBlob::end(){
auto e = StrBlobPtr(*this, data->size());
return e;
}

github

StrBlobPtr.h

#ifndef __STRBLOBPTR_H__
#define __STRBLOBPTR_H__ #include <memory>
#include <string>
#include <vector>
#include "StrBlob.h" class StrBlob;
class StrBlobPtr{
public:
StrBlobPtr() : curr(0){}
StrBlobPtr(StrBlob& a, size_t sz = 0):wptr(a.data), curr(sz){} //方法get和重载*的效果是一样的
std::string get(){
auto ptr = check(curr, "get string value");
return (*ptr)[curr];
} //方法get和重载*的效果是一样的
std::string& operator*(){
auto p = check(curr, "get string value");
return (*p)[curr];
}
std::string* operator->(){
return & this->operator*();
} StrBlobPtr& operator++();
StrBlobPtr& operator--();
StrBlobPtr operator++(int);
StrBlobPtr operator--(int); private:
std::shared_ptr<std::vector<std::string>>
check(std::size_t, const std::string&) const; std::weak_ptr<std::vector<std::string>> wptr;
std::size_t curr;
}; #endif

github

StrBlobPtr.cpp

#include "StrBlobPtr.h"

std::shared_ptr<std::vector<std::string>>
StrBlobPtr::check(std::size_t i, const std::string& msg) const{
auto ptr = wptr.lock();
if(!ptr){
throw std::runtime_error("unbound StrBlobPtr");
}
if(i >= ptr->size()){
throw std::out_of_range(msg);
}
return ptr;
} //qianzhi
StrBlobPtr& StrBlobPtr::operator++(){
check(curr, "will past end");
++curr;
return *this;
}
//qianzhi
StrBlobPtr& StrBlobPtr::operator--(){
--curr;
check(curr, "will past begin");
return *this;
}
//houzhi
StrBlobPtr StrBlobPtr::operator++(int){
auto tmp = *this;
++*this;
return tmp;
}
//houzhi
StrBlobPtr StrBlobPtr::operator--(int){
auto tmp = *this;
--*this;
return tmp;
}

github

main方法

#include "StrBlob.h"
#include "StrBlobPtr.h"
#include <iostream> using namespace std;
int main(){
StrBlob s1{"11", "22"};
StrBlobPtr p1 = s1.begin();
StrBlobPtr tm = ++p1;
cout << tm->size() << endl;
p1--;
tm = p1;
cout << *tm << endl;
}

编译方法:

g++ -g StrBlob.cpp StrBlobPtr.cpp mainStrBlobPtr.cpp -std=c++11

c/c++ 学习互助QQ群:877684253

本人微信:xiaoshitou5854

c/c++ 重载运算符 关系,下标,递增减,成员访问的重载的更多相关文章

  1. [C++ Primer] : 第14章: 重载运算符与类型转换

    基本概念 重载运算符是具有特殊名字的函数: 它们的名字由关键字operator和其后要定义的运算符号共同组成. 重载运算符函数的参数数量与该运算符作用的运算对象数量一样多. 对于二元运算符来说, 左侧 ...

  2. C++ 重载运算符(详)

    C++ 重载运算符 C 重载运算符 一重载函数 1例程 2备注 二重载运算符 11 二元运算符重载 11 一元运算符重载 111 -- 2备注 3 特殊运算符重载 31 号运算符 32 下标运算符 3 ...

  3. [C++] 重载运算符与类型转换(1)

      1.形式:返回值 operator符号(参数列表){}   2.不能被重载的运算符::: 作用域运算符  .*   . 成员访问运算符   ?: 条件运算符:某些运算符(逗号,,取地址&, ...

  4. 【C++】C++中重载运算符和类型转换

    输入输出运算符 输入输出运算符 输入输出运算符 算术和关系运算符 相等运算符 关系运算符 赋值运算符 复合赋值运算符 下标运算符 递增和递减运算符 成员访问运算符 函数调用运算符 lambda是函数对 ...

  5. C++重载运算符的规则

    (1)C++不允许用户自己定义新的运算符,只能对已有的C++运算符进行重载. 例如,有人觉得BASIC中用“* *”作为幂运算符很方便,也想在C++中将“* *”定义为幂运算符,用“3* *5”表示3 ...

  6. 【c++习题】【17/5/8】重载运算符

    1.设计一个Complex(复数)类,完成如下要求: 该类具有实部(Real_Part)和虚部(Image_Part)通过重载运算符“+”实现两个复数的相加通过重载运算符“+”实现一个复数与一个数值的 ...

  7. C++重载运算符简单总结

    当运算符作用于类类型的运算对象时,可以通过运算符重载重新定义该运算符的含义.明智的使用运算符重载能令我们的程序更易于编写和阅读. 一.基本概念 什么是运算符重载?重载的运算符是具有特殊名字的函数:它们 ...

  8. C++ Primer : 第十四章 : 重载运算与类型转换之重载运算符

    重载前须知 重载运算符是特殊的函数,它们的名字由operator和其后要重载的运算符号共同组成. 因为重载运算符时函数, 因此它包含返回值.参数列表和函数体. 对于重载运算符是成员函数时, 它的第一个 ...

  9. Chapter14:重载运算符

    对于一个运算符函数来说,它或者是类的成员,或者至少含有一个类类型的参数. int operator+(int, int);//错误,不能为int重定义内置运算符 对于一个重载的运算符来说,其优先级和结 ...

随机推荐

  1. Kubernetes 笔记 04 架构是个好东西

    本文首发于我的公众号 Linux云计算网络(id: cloud_dev),专注于干货分享,号内有 10T 书籍和视频资源,后台回复「1024」即可领取,欢迎大家关注,二维码文末可以扫. Hi,大家好, ...

  2. Go使用Makefile构建

    ​ 我们平常很多时候都是直接在命令行输入go build进行编译的: go build . 或者测试使用go run运行项目 go run main.go 我看有很多大型开源项目都是如下方式: mak ...

  3. HashMap? ConcurrentHashMap? 相信看完这篇没人能难住你!

    前言 Map 这样的 Key Value 在软件开发中是非常经典的结构,常用于在内存中存放数据. 本篇主要想讨论 ConcurrentHashMap 这样一个并发容器,在正式开始之前我觉得有必要谈谈 ...

  4. qt之fiddler抓包

    最近项目中使用到了Qt的网络库,在用的过程中也发现了不少坑和问题,本文仅仅作为记录,方便日后查阅.    因为我们整个客户端的gui都是使用qt来完成的,心想qt既然有网络库,而且真心觉着qt封装的控 ...

  5. .net好好地利用Conditional属性

    Conditional是.net提供关于编译的属性描述,其作用是添加到方法或属上,通过定义编译符的方式告指示编译器应忽略方法调用或属性.在.NET中Debug 和 Trace 类中的方法都添加了这属性 ...

  6. SpringBoot入门教程(二十)Swagger2-自动生成RESTful规范API文档

    Swagger2 方式,一定会让你有不一样的开发体验:功能丰富 :支持多种注解,自动生成接口文档界面,支持在界面测试API接口功能:及时更新 :开发过程中花一点写注释的时间,就可以及时的更新API文档 ...

  7. 从0打卡leetcode之day 6--最长回文串

    题目描述 给定一个字符串 s,找到 s中最长的回文子串.你可以假设 s 的最大长度为1000. 示例1 输入: "babad" 输出: "bab" 注意: &q ...

  8. SpringCloud入门之常用的配置文件 application.yml和 bootstrap.yml区别

    作者其他技术文章 1) Spring Boot 简介 2)SpringCloud入门之YAML格式文件规范学习 3)SpringCloud入门之Spring Boot多环境配置切换指南 4) Elas ...

  9. SmartSql 性能评测

    BenchmarkDotNet=v0.10.14, OS=Windows 10.0.17134 Intel Core i7-6700K CPU 4.00GHz (Skylake), 1 CPU, 8 ...

  10. H5 和 CSS3 新特性

    博客地址:https://ainyi.com/52 H5 新特性 语义化标签:header.footer.section.nav.aside.article 增强型表单:input 的多个 type ...