运算符重载
来源:互联网 发布:悠悠乎与颢气俱 编辑:程序博客网 时间:2024/06/06 02:35
一句话总结:究其本质,触类旁通。特别注意后置++/--的实现,如前置++和后置++的本质实现过程与重载的实现方法。
一元运算符重载:负、++、-- 运算符重载
二元运算符重载:加减
输出运算符重载
// COperator.hpp
// CppProduct
//
// Created by zhaojunyan on 17/6/18.
// Copyright © 2017年 zhaojunyan. All rights reserved.
//
#ifndef COperator_hpp
#define COperator_hpp
#include <stdio.h>
#include <iostream>
class COperator
{
friend COperator &operator--(COperator &c);//前置--
friend COperatoroperator--(COperator &c,int);//int是一个标志,告诉编译器这是一个后置--,需放在最后
friend COperator &operator-(const COperator c1,const COperator c2);//二元减运算符
friend std::ostream &operator<<(std::ostream &out, COperator &c);//输出运算符,不可为成员返回,属于ostream对象
public:
COperator();
~COperator();
/*一元运算符*/
COperator &operator-();
COperator &operator+();
COperator &operator++();
COperatoroperator++(int);//int是一个标志,告诉编译器这是一个后置++,
//注意后置++并不是在原有对象上直接+1,首次返回的是原始值,下次才是++的值,所以不要加引用,注意与前置++的区别
//COperator &operator--();
//COperator operator--(int);
/*二元运算符*/
COperator &operator+(constCOperator &c);//减运算类同
public:
int m_value;
};
#endif /* COperator_hpp */
// COperator.cpp
// CppProduct
//
// Created by zhaojunyan on 17/6/18.
// Copyright © 2017年 zhaojunyan. All rights reserved.
//
#include "COperator.hpp"
COperator::COperator(){}
COperator::~COperator(){}
COperator &COperator::operator-()
{
m_value = -m_value;
return *this;
}
COperator &COperator::operator+()
{
m_value = +m_value;
return *this;
}
COperator &COperator::operator++()
{
m_value = ++m_value;
return *this;
}
COperator COperator::operator++(int)
{
COperator old(*this);//使用默认拷贝构造函数谨防浅拷贝
m_value++;
return old;//返回原来的值,下次调用才是++后的值
}
//COperator &COperator::operator--()
//{
// --m_value;
// return *this;
//}
//
//COperator COperator::operator--(int)
//{
// COperator old(*this);
// m_value--;
// return old;
//}
COperator &COperator::operator+(constCOperator &c)
{
COperator outObj;
outObj.m_value =this->m_value + c.m_value;
return outObj;
}
COperator &operator--(COperator &c)
{
c.m_value = --c.m_value;
return c;
}
COperator operator--(COperator &c,int)
{
COperator old(c);
c.m_value = --c.m_value;
return old;
}
COperator &operator-(constCOperator c1, constCOperator c2)
{
COperator c3;
c3.m_value = c1.m_value - c2.m_value;
return c3;
}
std::ostream &operator<<(std::ostream &out, COperator &c)
{
out<<c.m_value;
return out;
}
// main.cpp
// CppProduct
//
// Created by zhaojunyan on 17/6/17.
// Copyright © 2017年 zhaojunyan. All rights reserved.
//
#include <iostream>
#include "COperator.hpp"
using namespacestd;
int main(int argc,const char * argv[]) {
// insert code here...
COperator coperator;
coperator.m_value =9;
-coperator;
cout<<coperator.m_value<<endl;
-coperator;
cout<<(coperator++).m_value<<endl;
cout<<coperator.m_value<<endl;
//cout<<(coperator--).m_value<<endl;
cout<<coperator.m_value<<endl;
cout<<(++coperator).m_value<<endl;
//cout<<(--coperator).m_value<<endl;
cout<<(--coperator).m_value<<endl;
cout<<(coperator--).m_value<<endl;
cout<<coperator.m_value<<endl;
COperator c1;
COperator c2;
c1.m_value =1;
c2.m_value =3;
COperator c3;
c3 = c1 + c2;
cout<<c3.m_value<<endl;
c3 =c2 - c1;
cout<<c3.m_value<<endl;
cout<<c3<<endl;;
return0;
}
- 运算符重载 [++,--,+,<<; &&,||(不可重载)]
- 重载之运算符重载
- 重载赋值运算符
- C++运算符重载
- 运算符重载
- 运算符重载总结
- 运算符重载例子
- 运算符重载
- 运算符重载
- 基础:运算符重载
- 运算符重载
- 运算符重载
- 重载赋值运算符
- C#运算符重载
- C++-运算符重载
- 重载赋值运算符
- 运算符重载
- 有关运算符重载
- Java-Arrays.asList()使用中的问题
- Git
- Python Tkinter 简单登录界面
- [Unity]已知圆点和射线 求射线与圆相交的一点
- Mac中打开命令行终端的方法
- 运算符重载
- Java实现-最大子数组差
- Cocos2d入门
- 小世界网络中的SIRS传染病模型实现
- Ubuntu下安装arm-linux-gcc-4.4.3.tar.gz (交叉编译环境)
- JSON与XML的区别比较
- 约瑟夫环问题
- 名企笔试:网易游戏雷火盘古2017实习生招聘笔试题(字符串编码)
- java运行环境总结