C++分析研究  
C++
日历
<2011年2月>
303112345
6789101112
13141516171819
20212223242526
272812345
6789101112
统计
  • 随笔 - 92
  • 文章 - 4
  • 评论 - 4
  • 引用 - 0

导航

常用链接

留言簿

随笔档案

文章档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

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

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

  1.#include < iostream>

  2.#include < cstdlib>

  3.using namespace std; www.601456.com

  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); www.liuhebao.com

  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); www.yzjxsp.com

  19.}

  20.T getM(){

  21.return m_;

  22.}

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

  24.A& operator ++ { www.yzyedu.com

  25.(this->m_)++;

  26.return *this;

  27.}

  28.// ++ 在后,这里返回的是一个新的A类型变量,且不可改变

  29.// 目的是防止出现 A++++情况

  30.const A operator ++(int a){

  31.A< T> b = *this;

  32.(this->m_)++;

  33.return b;

  34.}

  35.private:

  36.T m_;

  37.};

  38.int main(void){

  39.int i = 0; www.yzjjx.com

  40.cout< < ++++i< < endl;

  41.// i++++ is not allowed

  42.A< int> a;

  43.A< int> b = ++a;

  44.cout< < b.getM()< < endl;

  45.A< int> c = a++;

  46.cout< < c.getM()< < endl;

  47.cout< < a.getM()< < endl;

  48.int t = a+2; www.yzsws.com

  49.cout< < t< < endl;

  50.system("pause");

  51.return 0;

  52.} www.szfuao.com

  以上就是对C++操作符重载的相关介绍。
posted on 2011-02-20 23:42 HAOSOLA 阅读(146) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


 
Copyright © HAOSOLA Powered by: 博客园 模板提供:沪江博客
PK10开奖 PK10开奖