#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