C++学习笔记 lesson5 运算符重载
来源:互联网 发布:js判断字符串不相等 编辑:程序博客网 时间:2024/05/06 03:43
模拟所有的int
#include<iostream>//模拟所有的intclass Integer{public:Integer(int num = 0) :_num(num) {}// & 取地址运算符 空类中默认生成 // * 空类中默认生成 //无法重载的运算符// :: 域运算符 ?: 三目运算符 .直接成员访问运算符 sizeof //Integer &operator=(const Integer& other)//{//std::cout << "Integer& operator=(const Integer& " << _num << " )" << std::endl;//_num = other._num;//return *this;//} Integer& operator=(int num){std::cout << "Integer& operator=(int " << num << " )" << std::endl;_num = num;return *this;}Integer&operator+(int num){Integer temp = _num; //使用临时变量,否则加后本身为自增temp._num += num;return *this;}//友元是一种特殊的机制,不属于当前类的任何一个成员函数//使用对象是不能.出来的//主要用于运算符的重载friend Integer&operator+(int num, Integer&me){Integer temp=me._num;temp._num += num;return me;}Integer& operator++() //前++{_num += 1;return *this;//std::cout << "operator++()" << std::endl;}Integer& operator++(int) //后++{Integer temp = *this;_num += 1;return temp; //返回+之前的临时对象//std::cout << "operator++(int)" << std::endl;}bool operator==(const Integer& other)const{return _num == other._num;}friend std::ostream& operator <<(std::ostream& os, const Integer &me) {os << me._num;return os;}friend std::istream&operator >> (std::istream&lhs, const Integer&rhs){lhs >> rhs._num;return lhs;}int GetNum(){return _num;}private:int _num; };int main(){Integer demo = 10,other=20;demo = other = 100;//other = 100;==> other.operator=(100);//demo = other;==> demo.operator=(other);//===> demo.operator=(other.operator=(100))//Integer& operator=(int 20) 先调用other//Integer& operator=(const Integer&)10 再调用默认的demo = 100 + other + 1000;// friend operator+// operator+(int)// 赋值return 0;}
0 0
- C++学习笔记 lesson5 运算符重载
- C语言提高班学习笔记 lesson5 编译
- 【笔记-C++】 运算符重载
- 运算符重载 学习笔记
- 运算符重载学习笔记
- 【C++】【学习笔记】【025-027】运算符重载
- 【C++】学习笔记五十一——运算符重载
- C语言学习笔记 lesson5 浮点数类型及复习
- C语言提高班学习笔记 lesson5 变量、指针
- 《Inside C#》笔记(十一) 运算符重载
- 学习C++——运算符重载
- C++primer学习:重载运算符(3)
- C++primer学习:重载运算符(4)
- c#学习笔记(七)运算符重载
- C++学习笔记之运算符重载
- C++学习笔记十四-运算符重载
- c++学习笔记(运算符重载)
- Thinkcpp 学习笔记 运算符重载
- 使用列表解析式输出以下结果 ''' ['0000', '0001', '0002', '0003', '0004', '0005', '0006', '0007', '0008', '0009'] '
- iOS_调试_error: src refspec master does not match any.
- Android-apktool反汇编异常-Input file (XXX) was not found or was not readable.
- 画板UI的实现
- Spring Bean扩展接口 -- InitializingBean源码分析
- C++学习笔记 lesson5 运算符重载
- 【Unity&NGUI】背包拖拽问题&物体穿透
- PHP中Imagick类的使用(转)
- bzoj3876 [Ahoi2014]支线剧情
- Xamarin.iOS UIView移除子View
- 开通mysql root 用户远程访问权限
- React Native ListView警告:in next release empty section headers ...
- jQuery中设置html的外部样式表的奇偶行交替色类似
- windows向远程linux主机传送文件