通过引用设置 Bool 的默认值
如何为作为引用的 bool 分配默认值?
我有一个这样的函数:
void someFunction(bool a = true, bool& b)
{
if(a)
b = false;
else
std::cout << "nothings changed" << std::endl;
}
在这种情况下我应该如何将默认值分配给 b ?
void someFunction(bool a = true, bool& b = false)
不会起作用。那么应该怎么做呢?
How do i assign a default value to a bool that is a reference?
I have a function like this :
void someFunction(bool a = true, bool& b)
{
if(a)
b = false;
else
std::cout << "nothings changed" << std::endl;
}
How should i assign the default value to b in this context?
void someFunction(bool a = true, bool& b = false)
will not work. So how should it be done?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您不能使用值初始化非常量引用。你需要一个变量:
但是简单地返回 bool 可能会更有意义:
然后客户端仍然可以自己决定是否要忽略结果。
You cannot initialize non-const references with values. You need a variable:
But it would probably make a lot more sense to simply return the bool:
Then the client can still decide for himself if he wants to ignore the result or not.
您不能将临时对象绑定到非常量引用。您需要使用非临时对象:
You cannot bound temporary object to a non-const reference. You need to use non-temp object:
为什么不使用指针呢?
Why not use a pointer?