int转float的问题
这是我的问题。我有一个构造函数,它使颜色从 4 个浮点范围为 0 到 1。我想添加与 0 到 255 int 的兼容性,所以我有另一个像这样的构造函数:
AguiColor::AguiColor( int r, int g, int b, int a )
{
double num = 1.0f / 255.0f;
AguiColor((float)(r * num), (float)(g * num), (float)(b * num), (float)(a * num));
}
但这不起作用。 rgba 浮点组件变成奇怪的数字。这有什么问题吗?
谢谢
Here is my issue. I have a constructor that makes a color from 4 floats ranging 0 to 1. I want to add compatibility with 0 to 255 int so I have another constructor like this:
AguiColor::AguiColor( int r, int g, int b, int a )
{
double num = 1.0f / 255.0f;
AguiColor((float)(r * num), (float)(g * num), (float)(b * num), (float)(a * num));
}
However this does not work. The rgba float components become strange numbers. What is wrong with this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
C++03 不支持构造函数委托(a/k/a 链接)。当您调用其他 Java 风格的构造函数时,它会创建一个临时对象,而不会影响正在构造的对象。
这可能会解决这个问题,但不如直接初始化成员那么有效。
C++03 doesn't support constructor delegation (a/k/a chaining). When you call that other constructor, Java-style, it creates a temporary object, without affecting the object under construction.
This might fix it, but isn't as efficient as initializing the members directly would be.