1

这是我的问题。我有一个构造函数,它使颜色从 0 到 1 的 4 个浮点数。我想添加与 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 浮点组件变成奇怪的数字。这有什么问题?

谢谢

4

1 回答 1

4

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));
}
于 2010-11-03T00:23:00.913 回答