如何在 const 函数内调用非常量函数 (C++)
我有一个如下所示的遗留函数:
int Random() const
{
return var_ ? 4 : 0;
}
我需要在该遗留代码中调用一个函数,以便它现在看起来像这样:
int Random() const
{
return var_ ? newCall(4) : 0;
}
问题是我收到此错误:
In member function 'virtual int Random() const':
class.cc:145: error: passing 'const int' as 'this' argument of 'int newCall(int)' discards qualifiers
现在我知道为了修复此错误我可以使我的 newCall() 成为 const 函数。但随后我必须在 newCall()
中进行几个函数调用,因此现在我必须将所有这些函数调用设为 const。依此类推,直到最终我觉得我的程序有一半将是 const。
我的问题:有什么方法可以调用 Random() 中非 const 的函数吗?或者是否有人对如何在 Random()
中实现 newCall()
而不使我的程序的一半成为常量有任何想法。
谢谢
-乔什
I have a legacy function that looks like this:
int Random() const
{
return var_ ? 4 : 0;
}
and I need to call a function within that legacy code so that it now looks like this:
int Random() const
{
return var_ ? newCall(4) : 0;
}
The problem is that I'm getting this error:
In member function 'virtual int Random() const':
class.cc:145: error: passing 'const int' as 'this' argument of 'int newCall(int)' discards qualifiers
Now I know in order to fix this error I can make my newCall()
a const function. But then I have several funciton calls in newCall()
that I have to make, so now I would have to make all of those function calls const. And so on and so forth until eventually I feel like half my program is going to be const.
My question: is there any way to call a function within Random() that isn't const? Or does anyone have any ideas on how to implement newCall()
within Random()
without making half my program const.
Thanks
-josh
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
但这不是一个好主意。如果可以的话就避免吧!
But it's not a good idea. Avoid if it's possible!
你应该改变你的程序以正确使用/声明const...
一种替代方法是使用const_cast。
you should alter your program to use/declare const correctly...
one alternative is to use const_cast.
仅当您确定 newCall 不会修改“this”时才执行此操作。
Only do this if you're certain newCall will not modify "this".
这里有两种可能性。首先,newCall 及其所有被调用者实际上都是非修改函数。在这种情况下,您绝对应该仔细检查并将它们全部标记为
const
。您和未来的代码维护人员都会感谢您使阅读代码变得更加容易(从这里的个人经验来看)。其次,newCall 实际上会改变对象的状态(可能通过它调用的函数之一)。在这种情况下,您需要破坏 API 并使Random
成为非 const,以正确地向调用者指示它修改了对象状态(如果修改仅影响物理常量而不影响逻辑常量,则可以使用可变属性并传播const
)。There are two possibilities here. First,
newCall
and ALL of its callees are in fact non-modifying functions. In that case you should absolutely go through and mark them allconst
. Both you and future code maintainers will thank you for making it much easier to read the code (speaking from personal experience here). Second,newCall
DOES in fact mutate the state of your object (possibly via one of the functions it calls). In this case, you need to break API and makeRandom
non-const to properly indicate to callers that it modifies the object state (if the modifications only affect physical constness and not logical constness you could use mutable attributes and propagateconst
).如果不使用 const 强制转换,您可以尝试在
Random()
方法中创建该类的新实例吗?Without using const casts, could you try creating a new instance of the class in the
Random()
method?const 限定符断言类的实例 this 在操作后将保持不变,这是编译器无法自动推断出的。
const_cast
可以使用,但它是邪恶的The
const
qualifier asserts that the instancethis
of the class will be unchanged after the operation, something which the compiler cant automagically deduce.const_cast
could be used but its evil如果它确实是一个随机数生成器,那么数字生成代码/状态可能会放置在类本地静态生成器中。这样,您的对象就不会发生变化,并且该方法可以保持常量。
if it's really a random number generator, then the number generation code/state could likely be placed in a class-local static generator. this way, your object is not mutated and the method may remain const.