int转float的问题

发布于 2024-09-30 20:08:33 字数 334 浏览 0 评论 0原文

这是我的问题。我有一个构造函数,它使颜色从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那请放手 2024-10-07 20:08:33

C++03 不支持构造函数委托(a/k/a 链接)。当您调用其他 Java 风格的构造函数时,它会创建一个临时对象,而不会影响正在构造的对象。

这可能会解决这个问题,但不如直接初始化成员那么有效。

AguiColor::AguiColor( int r, int g, int b, int a )
{
  double num = 1.0f / 255.0f;
  *this = AguiColor((float)(r * num), (float)(g * num), (float)(b * num), (float)(a * num));
}

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.

AguiColor::AguiColor( int r, int g, int b, int a )
{
  double num = 1.0f / 255.0f;
  *this = AguiColor((float)(r * num), (float)(g * num), (float)(b * num), (float)(a * num));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文