C++ class 作为静态值的命名空间

C++ class as namespace for static values

是否有相同的 API 来自带有 class

的命名空间

命名空间示例:

namespace Direction
{
    static const vec3 up = vec3(0, 1, 0);
}

// in code
{
   vec3 v = Direction::up;
}

而使用 class 我必须使用方法

class Color : public vec4
{
    // class stuff //

    static Color black() {return Color(0, 0, 0, 1);}
}

// in code
{
   Color c = Color::black();
}

我希望能够使用 Color c = Color::black;,颜色为 class,黑色在 header 中定义。有什么办法吗?

编辑:另一种表达方式是: 为什么这会给我“不允许不完整的类型”和“'const Color' 的成员不能有和 in-class 初始值设定项”

class Color : public glm::vec4
{
public:
    explicit Color(const float r, const float g, const float b, const float a = 1.0f)
        : glm::vec4(r, g, b, a) {}

    static const Color a = Color(1, 1, 1, 0); // error
}

试试这个

class Color 
{
    public:
    Color(int x, int y, int z, int t)
    {}
    const static Color BLACK;
};
const Color Color::BLACK{1,2,3,4};

int main()
{
   Color c = Color::BLACK;
}

我在 https://godbolt.org/z/4oP55jTMK

测试过