C++中的运算符重载(二)
来源:互联网 发布:淘宝详情页和主图视频 编辑:程序博客网 时间:2024/05/24 07:45
1.C++中重载++操作符,
C++中重载前++操作符,使用成员函数重载,
,用如下形式:
<类名> operator ++()
{
···// 函数体
}
用成员函数重载后++操作符,形式为:
<类名> operator ++(int)
{
···// 函数体
}
它们的区别在于,后++操作符的形参表多了一个int,在调用时,前++操作符的调用方法为 对象名.operator++(),后++的调用方法为 对象名.operator ++(0)。
下面的代码实现了重载++ -- [] *操作符。
# include <iostream>using namespace std;# include <vector>template<typename T>class Check{public:Check(T *b,T* e):beg(b),end(e),curr(b){}Check &operator ++(); Check &operator --();Check &operator ++(int);Check &operator --(int);T& operator [](const size_t index);const T& operator [](const size_t index) const;T &operator*() const;private:T *beg;T *end;T *curr;};template <typename T>Check <T> & Check<T>::operator++(){if(curr==end)throw out_of_range("increment past the end of check");curr++;return *this;}template <typename T>Check <T>& Check<T>::operator--(){if(curr==beg)throw out_of_range("decrement past the head of check");curr--;return *this;}template <typename T>Check <T>& Check<T>::operator++(int){Check t(*this);++*this;return t;}template <typename T>Check <T> & Check <T>::operator--(int){Check t(*this);--(*this);return t;}template <typename T>T& Check <T>::operator[](const size_t index){if (beg+index>end)throw out_of_range("out of range");return *(beg+index);}template <typename T>const T & Check <T>::operator[](const size_t index) const{if(beg+index>end)throw out_of_range("index out of range");return *(beg+index);}template <class T>T & Check<T>::operator *() const{if(curr==end)throw out_of_range("index out of range");return *curr;}int main(){int a[10]={1,2,3,4,5,6,7,8,9,0};const Check <int> t(a,a+10);Check <int> t2(a,a+10);Check <int> t3(a,a+3);cout<<*t<<endl;t3=t2.operator++();cout<<*t3<<endl;t2.operator--();Check <int> t4(a,a+5);t4=t2.operator++(0);cout<<*t4<<endl;cout<<t4.operator[](2)<<endl;cin.get();return 0;}
2 重载<<操作符
当定义符合标准库iostream规范的输入或输出操作符的时候,必须定义为友元操作符,否则,左操作数将只能是该类类型的对象。
Sales_item item;
Item <<cout;
这个用法与为其它类型定义的输出操作符的正常使用方式相反。类通常将IO操作符设为友元。
# include <iostream>using namespace std;class test{public:test(int i):x(i){}friend ostream& operator<<(ostream & os,test & a);int getx(){return x;}private:int x;}; ostream& operator<<(ostream & os,test & a){os<<a.getx()<<endl;return os;}int main(){test a(1);cout<<a;cin.get();return 0;}
0 0
- C++中的运算符重载(二)
- 第9周-运算符重载(二)-复数类中的运算符重载(续)
- Time类中的运算符重载(1)-二目的比较运算符重载
- Time类中的运算符重载(2)-二目运算符的重载
- C++——运算符重载(二)
- 【C/C++开发】运算符重载二
- 运算符重载(二)
- 运算符重载(二)
- OJ矩形类中的运算符重载(C++)
- 【C++】String类中的运算符重载
- 运算符重载(c++)
- 第九周项目二——Time类中的运算符重载(利用输入输出运算符重载)
- 从零开始学C++之运算符重载(二):++运算符重载、!运算符重载、赋值运算符重载
- 从零开始学C++之运算符重载(二):++运算符重载、!运算符重载、赋值运算符重载
- 从零开始学C++之运算符重载(二):++运算符重载、!运算符重载、赋值运算符重载
- 重载赋值运算符(c/c++)
- C++-运算符重载
- [C++]重载运算符
- 关于stream!=NULL问题
- ibatis-2.3.3学习
- 【Java】如何终止一个线程
- sicily 1031.Campus
- boost多线程库使用指南
- C++中的运算符重载(二)
- Invalid file name: must contain only [a-z0-9_.]
- hadoop简介
- WPF下、使用Double作为点坐标带来的问题或line直线颜色不明显的问题的解决方法
- 【MoreWindows工作笔记11】EnumClipboardFormats 剪切板内容的数据格式
- Lucene 的Query Parser(查询语法)
- TRUNC函数的用法
- 内核文件系统的注册
- Boost多线程编程