重载与const

来源:互联网 发布:win7 64位c语言编译器 编辑:程序博客网 时间:2024/05/22 00:26

#ifndef _OVERLOAD_TEST_H_
#define _OVERLOAD_TEST_H_

class COverloadTest
{
public:
    COverloadTest() : m_nValue(0) {}
    ~COverloadTest() {}
public:
    const int getValue() const
    {

        cout << "const getValue" << endl;
        return m_nValue;
    }

    int getValue()
    {

        cout << "getValue" << endl;
        return m_nValue;
    }
private:
    int m_nValue;
};

#endif

 

调用:

COverloadTest test;
const int value = test.getValue();

 

上述代码可以编译通过,但是在执行时是调用的非const类型的成员函数。

输出为:

getValue