有没有办法更改 const 结构的成员?
我想更改 const
函数参数的成员。想象一下我正在处理的纹理类中的这个片段:
template<>
void mySuperTexture::loadPixels<float>(uint32 _width, uint32 _height, float * _pixels, const Settings & _settings)
{
if (_settings.pixelDataType == PixelDataType::Auto) {
_settings.pixelDataType = PixelDataType::Float32;
// ^ obviously this does not work on the const variable
}
// ...
m_pimpl->loadPixels(_width, _height, _pixels, _settings);
}
我知道我可以使参数成为非const,但这不允许我为设置参数指定默认参数,或者调用带有临时变量的函数。
我想我可以在 loadPixels 函数内复制设置struct
,但我想避免这种情况。还有其他想法吗?
编辑:
好吧,我决定使用可变关键字。-原因如下:在我的场景中,Settings 结构基本上仅在构建纹理/加载像素期间使用,之后不存储.- 因此,mutable
不会改变用户的常量,因为它根本无法访问。在我的情况下,重载似乎确实更痛苦,因为我正在为各种类型(即无符号字符、短整型、整数...)实现 loadPixels 方法,因此仅重载就会增加源代码量很多。顺便提一句。实际的设置保存在 pimpl 对象中(其 loadPixel 函数不是模板化的,因此不知道有关类型的任何信息),这就是为什么我想直接在 const Settings 结构上工作。
I want to change a member of a const
function argument. imagine this snippet from a texture class I am working on:
template<>
void mySuperTexture::loadPixels<float>(uint32 _width, uint32 _height, float * _pixels, const Settings & _settings)
{
if (_settings.pixelDataType == PixelDataType::Auto) {
_settings.pixelDataType = PixelDataType::Float32;
// ^ obviously this does not work on the const variable
}
// ...
m_pimpl->loadPixels(_width, _height, _pixels, _settings);
}
I know that I could make the argument non-const
, but that would not allow me to specify a default argument for the settings argument, or call the function with a temp variable.
I guess I could make a copy of the Settings struct
inside the loadPixels function but I'd like to avoid that. Any other ideas?
EDIT:
Okay, I decided to go with the mutable keyword.- Here is why: In my scenario the Settings struct is basically only used during the construction of the texture/loading of the pixels and not stored afterwards.- Thus the mutable
does not change anything about the constness for the user since its simply not accessible. Overloading does seem to be more of a pain in my situtation since I am implementing the loadPixels method for various types( i.e. unsigned char, short, int ...) thus just overloading would increase the amount of source code alot. Btw. the actuall settings get saved inside a pimpl object (whose loadPixel function is not templated and thus does not know anything about the type) thats why I want to work directly on the const Settings struct.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您有两个选择:
const_cast <> ()
mutable
但是,当您想要更新 const-y 和非 const 内容时,我猜您想使用非 const 版本重载该函数:
并且然后将公共代码放入另一个函数中。
You have two options:
const_cast <> ()
mutable
But, as you want to update const-y and non-consty things, I guess you want to overload the function with a non-const version:
and then put common code into another function.
我不清楚为什么你应该修改设置参数。
其他建议似乎不太合理。为了修改 const 对象而放弃常量会导致未定义的行为。此外,让完全普通的数据成员可变也不是一个好主意,因为这会使 const 关键字失去大部分价值。
如果您想避免复制但存储对设置的引用/指针,这不是一个好主意,因为设置对象的生命周期很容易比纹理对象短。制作副本将在此处提供最健壮的代码。
作为替代方案,您可以完全摆脱
PixelDataType::Auto
,并让Settings
将此成员初始化为Float32
本身(例如默认情况下)。It is unclear to me why you should be modifying the settings parameter.
Other suggestions don't seem sound. Casting constness away in order to modify a const object leads to undefined behavior. Also it is not a good idea to make perfectly ordinary data members mutable, as it would make the const keyword lose much of its value.
If you want to avoid a copy but store a reference/pointer to the Settings, this is not a good idea, because the lifetime of the Settings object can easily be shorter than the texture object. Making a copy would give most robust code here.
As an alternative you could get rid of
PixelDataType::Auto
altogether, and haveSettings
initialize this member toFloat32
itself (e.g. by default).如果你真的知道你在做什么,你可以抛弃常量(参见 http:// www.cplusplus.com/doc/tutorial/typecasting/ ),但使用 const 强制转换几乎总是糟糕设计的标志。
If you really know what sou're doing, you could cast the constness away (see http://www.cplusplus.com/doc/tutorial/typecasting/ ), but using const cast is almost always a sign of a bad design.