如何在 const 函数中调用非常量函数 (C++)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5008541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to call a non-const function within a const function (C++)
提问by Grammin
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.
现在我知道为了修复这个错误,我可以创建newCall()
一个 const 函数。但是后来我必须进行几个newCall()
函数调用,所以现在我必须使所有这些函数调用都为 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.
我的问题:有没有办法在 Random() 中调用一个不是 const 的函数?或者有人对如何newCall()
在Random()
不使我的一半程序为常量的情况下实现有任何想法。
Thanks
谢谢
-josh
-乔希
采纳答案by justin
you shouldalter your program to use/declare const correctly...
您应该更改您的程序以正确使用/声明 const ...
one alternative is to use const_cast.
一种替代方法是使用 const_cast。
回答by Nawaz
int Random() const
{
return var_ ? const_cast<ClassType*>(this)->newCall(4) : 0;
}
But it's not a good idea. Avoid if it's possible!
但这不是一个好主意。如果可能,请避免!
回答by Erik
const_cast<MyClass *>(this)->newCall(4)
Only do this if you're certain newCall will not modify "this".
仅当您确定 newCall 不会修改“this”时才执行此操作。
回答by Mark B
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 all const
. 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 make Random
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 propagate const
).
这里有两种可能。首先,newCall
它的所有被调用者实际上都是非修改函数。在这种情况下,您绝对应该检查并标记所有内容const
。你和未来的代码维护者都会感谢你让阅读代码变得更加容易(这里是根据个人经验说的)。其次,newCall
确实会改变对象的状态(可能通过它调用的函数之一)。在这种情况下,您需要中断 API 并制作Random
非常量以正确地向调用者指示它修改了对象状态(如果修改仅影响物理常数而不影响逻辑常数,您可以使用可变属性和传播const
)。
回答by justin
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.
如果它真的是一个随机数生成器,那么数字生成代码/状态可能会被放置在一个类本地静态生成器中。这样,您的对象不会发生变异,并且该方法可能会保持不变。
回答by Coffee on Mars
Without using const casts, could you try creating a new instance of the class in the Random()
method?
如果不使用 const 强制转换,您可以尝试在Random()
方法中创建类的新实例吗?
回答by Foo Bah
The const
qualifier asserts that the instance this
of the class will be unchanged after the operation, something which the compiler cant automagically deduce.
在const
预选赛中称,该实例this
的类会操作,一些东西,编译器自动的比赛不能上场之后的演绎是不变的。
const_cast
could be used but its evil
const_cast
可以使用,但它的邪恶