一句话总结:究其本质,触类旁通。特别注意后置++/--的实现,如前置++和后置++的本质实现过程与重载的实现方法。
一元运算符重载:负、++、-- 运算符重载
二元运算符重载:加减
输出运算符重载
// 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 COperator operator--(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++();
COperator operator++(int);//int是一个标志,告诉编译器这是一个后置++,
//注意后置++并不是在原有对象上直接+1,首次返回的是原始值,下次才是++的值,所以不要加引用,注意与前置++的区别
//COperator &operator--();
//COperator operator--(int);
/*二元运算符*/
COperator &operator+(const COperator &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+(const COperator &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-(const COperator c1, const COperator 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 namespace std;
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;;
return 0;
}