如何在其内部创建类的(静态常量?)实例(即 Color 类)?
我正在尝试创建一个颜色类。例如:
class Color
{
public:
float r, g, b, a;
Color(float r_, float g_, float b_, float a_);
...
}
为了方便起见,我想将类的实例包含为类的一部分,这样我就不需要为常见颜色指定 rgba 值:
image.setPixel(100, 100, Color::Red);
但是,我不知道正确的语法是什么这是。我在谷歌上搜索了一段时间,但没能找到这个方法的术语:(。我之前在其他库中见过它,所以我认为这是可能的。我认为它有被声明为静态常量,但我不确定这一点:
class Color
{
public:
static const Color Red;
...
}
const Color::Color Red = Color(255, 0, 0, 255);
但我得到一个
错误提示 Color::Color 是无效类型
我做错了什么?
I'm trying to create a class for colors. Something like:
class Color
{
public:
float r, g, b, a;
Color(float r_, float g_, float b_, float a_);
...
}
And for convenience I would like to include instances of the class as part of the class so that I don't need to specify the rgba values for common colors as such:
image.setPixel(100, 100, Color::Red);
However, I don't know what the correct syntax for this is. I've been searching on Google for a while and I haven't been able to find the term for this method :(. I've seen it done in other libraries before, so I think it's possible. I'm thinking it has to be declared as a static constant, but I'm not sure about this:
class Color
{
public:
static const Color Red;
...
}
const Color::Color Red = Color(255, 0, 0, 255);
But I get an
error saying Color::Color is an invalid type
What am I doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
红色的是会员,应该是:
Red is the member, it should be: