C++操作符重载不同方式的区别是什么

C++操作符重载不同方式的区别是什么 ,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

创新互联建站2013年开创至今,先为周至等服务建站,周至等地企业,进行企业商务咨询服务。为周至企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。

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

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

#include < iostream> #include < cstdlib> using namespace std;  template< typename T> class A  {  public:  A(): m_(0){  }  // +  const T operator + (const T& rhs)  {  // need to be repaired , but see it is only a demo  return (this->m_ + rhs);  }  // -  const T operator - (const T& rhs){  // need to be repaired , but see it is only a demo  return (this->m_ - rhs);  }  T getM(){  return m_;  }  // ++在前的模式,这里返回的是引用 ,准许++++A  A& operator ++ (){  (this->m_)++;  return *this;  }  // ++ 在后,这里返回的是一个新的A类型变量,且不可改变  // 目的是防止出现 A++++情况  const A operator ++(int a){  A< T> b = *this;  (this->m_)++;  return b;  }  private:  T m_;  };  int main(void){  int i = 0;  cout< < ++++i< < endl;  // i++++ is not allowed  A< int> a;  A< int> b = ++a;  cout< < b.getM()< < endl;  A< int> c = a++;  cout< < c.getM()< < endl;  cout< < a.getM()< < endl;  int t = a+2;  cout< < t< < endl;  system("pause");  return 0;   }

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注创新互联行业资讯频道,感谢您对创新互联的支持。


网页标题:C++操作符重载不同方式的区别是什么
网页URL:http://myzitong.com/article/pehdhh.html