重载运算符 关系,下标,递增减,成员访问的重载
为了演示关系,下标,递增减,成员访问的重载,创建了下面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
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;
}
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
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;
}
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