Mr *_*old 6 c++ methods const class function
class A
{
public:
A(){};
~A(){};
void method(){};
};
void call(const A &a)
{
a.method(); // I cannot call this method here if I use "const" but I can call it if not using "const"
}
int main()
{
A a;
call(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,错误是:" passing const A as this argument of void A::method() discards qualifiers [-fpermissive]|"
在功能上call,如果我使用const,我得到错误,但如果我摆脱它,它的工作原理.
有谁可以帮我解释一下?
jua*_*nza 18
您不能通过const引用调用非const成员函数.您可以通过创建成员函数来解决此问题const:
void method() const {};
^^^^^
Run Code Online (Sandbox Code Playgroud)
这表明调用成员不会改变调用它的对象*
*从概念上讲.在实践中,它可以改变标记的成员mutable
| 归档时间: |
|
| 查看次数: |
7681 次 |
| 最近记录: |