首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

++操作符重载,该如何处理

2013-10-21 
++操作符重载C++的重载操作符,这里以++操作符为例。class Test{public:Test(x3){ m_value x}Test &opera

++操作符重载
C++的重载操作符,这里以++操作符为例。

class Test

{

    public:

    Test(x=3){ m_value = x}

    Test &operator ++();   //前增量

    Test &operator ++(int);//后增量

private:

    Int m_value:

};

Test &Test::operator ++()

{

    m_value ++;    //先增量

    return *this;  //返回当前对象

}

Test Test::operator ++(int)

{

    Test tmp(*this);  //创建临时对象

    m_value ++;       //再增量

    return temp;      //返回临时对象

}


以上的两个++重载操作符的函数中,都用到了m_value ++,这里的++是什么意思?
明明在写Test类的重载++操作符,却在函数内部使用了++。
我猜想,m_value ++中由于m_value是int型,其++实内置的,故可用?
c++
[解决办法]
引用:
C++的重载操作符,这里以++操作符为例。
class Test

{

    public:

    Test(x=3){ m_value = x}

    Test &operator ++();   //前增量

    Test &operator ++(int);//后增量

private:

    Int m_value:

};

Test &Test::operator ++()

{

    m_value ++;    //先增量

    return *this;  //返回当前对象

}

Test Test::operator ++(int)

{

    Test tmp(*this);  //创建临时对象

    m_value ++;       //再增量

    return temp;      //返回临时对象

}


以上的两个++重载操作符的函数中,都用到了m_value ++,这里的++是什么意思?
明明在写Test类的重载++操作符,却在函数内部使用了++。
我猜想,m_value ++中由于m_value是int型,其++实内置的,故可用?

楼主你觉得Test类的重载++操作符影响其他类的对象++?
[解决办法]
你理解的正确。int的++是内置的

热点排行