C++操作符重载不同方式区别

2014-11-14 15:00:26 · 作者: · 浏览: 16

  C++ 编程语言可以被看做是C语言的升级版本,它能够支持C语言中的所有功能,而且在其他方面也有很大的提升。其中,在C++操作符重载中++,--需要说明是++(--)在操作数前面,还是在操作数后面,区别如下:


  C++操作符重载代码经过测试无误(起码我这里没问题^_^)


  1.#include < iostream>


  2.#include < cstdlib>


  3.using namespace std;


  4.template< typename T> class A


  5.{


  6.public:


  7.A(): m_(0){


  8.}


  9.// +


  10.const T operator + (const T& rhs)


  11.{


  12.// need to be repaired , but see it is only a demo


  13.return (this->m_ + rhs);


  14.}


  15.// -


  16.const T operator - (const T& rhs){


  17.// need to be repaired , but see it is only a demo


  18.return (this->m_ - rhs);


  19.}


  20.T getM(){


  21.return m_;


  22.}


  23.// ++在前的模式,这里返回的是引用 ,准许++++A


  24.A& operator ++ (){


  25.(this->m_)++;


  26.return *this;


  27.}