這初始化它是我現在有:聲明這個類聲明中一個類的實例,並就地
class CColorf
{
public:
CColorf();
CColorf(float r, float g, float b, float a = 1.0f);
public:
float r, g, b, a;
// predefined colors
// rgb(0.0, 0.0, 1.0)
static const CColorf blue;
};
它的工作原理與ccolorf.cpp定義像這樣blue
:
CColorf const CColorf::blue = CColorf(0.0f, 0.0f, 1.0f);
這是我想要做的是什麼:
class CColorf
{
...
// predefined colors
// rgb(0.0, 0.0, 1.0)
static const CColorf blue = CColorf(0.0f, 0.0f, 1.0f);
};
但它會產生一個編譯錯誤:
a static data member with an in-class initializer must have non-volatile const integral type
有沒有辦法避免這裏需要單獨的聲明和定義?
聲明吧'constexpr'。 'CColof'是一種文字類型,因此應該可以工作。 – Columbo 2015-02-05 14:56:07
@Columbo:不是'constexpr'應該用於表達式(或函數),而不是聲明? – 2015-02-05 14:57:22
...不知道你的意思,但你聽起來很迷惑。 'constexpr'是一個decl-specifier(聲明說明符),因此只能在聲明中使用。 – Columbo 2015-02-05 14:59:02