- 论坛徽章:
- 0
|
#include <string>
using std::string;
class A
{
public:
inline const string& get()
{
return m_str;
}
private:
string m_str;
};
class B
{
public:
void fun(const A& a)
{
string tmp = a.get();
}
};
|
在linux下g++编译报错
const.cpp: In member function 'void B::fun(const A&)':
const.cpp:21: error: passing 'const A' as 'this' argument of 'const std::string& A::get()' discards qualifiers
在solaris下报错
"const.cpp", line 21: Error: Non-const function A::get() called for const object.
而如果在get函数后面加上const就好了,这是怎么回事
另外如果不是作为函数参数是没有问题的 |
|