从const成员函数中调用非const成员函数 [英] Call a non-const member function from a const member function
本文介绍了从const成员函数中调用非const成员函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道是否有可能从const成员函数中调用非const成员函数。在下面的示例中,首先给出了编译器错误。我知道为什么会出现错误,我想知道是否有解决方法。
I would like to know if its possible to call a non-const member function from a const member function. In the example below First gives a compiler error. I understand why it gives an error, I would like to know if there is a way to work around it.
class Foo
{
const int& First() const
{
return Second();
}
int& Second()
{
return m_bar;
}
int m_bar;
}
我真的不想讨论这样做的智慧,我是
I don't really want to discuss the wisdom of doing this, I'm curious if its even possible.
推荐答案
return (const_cast<Foo*>(this))->Second();
然后安静地哭泣。
这篇关于从const成员函数中调用非const成员函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文