简体   繁体   English

C ++静态const类成员初始化

[英]C++ static const class members initialization

I'd like to have a class that has static members to itself, but I can't figure how to do that. 我想拥有一个本身具有静态成员的类,但是我不知道该怎么做。 Is that even possible? 那有可能吗?

I get the error: 我得到错误:

only static const integral data members can be initialized within a class 一个类中只能初始化静态const整数数据成员

Code: 码:

namespace misc
{
    class CData
    {
    public:
        CData( ) { };
        CData( int d );

        CData& operator = ( const CData& d );

        static const CData FIRST = CData( 512 ); //how?

    private:
        int data;
    };
}

As I use FIRST a lot I would like to statically access it using misc::CData::FIRST without the need to declare it somewhere in the scope. 由于我经常使用FIRST因此我想使用misc::CData::FIRST静态访问它,而无需在范围内的某个地方声明它。 Is that by any chance possible? 那有可能吗?

... without the need to declare it somewhere in the scope. ...无需在范围内的某个地方声明它。 Is that by any chance possible? 那有可能吗?

No, it's not possible without declaring it (which you already tried to do in your class declaration). 不,没有声明就不可能(您已经在类声明中尝试过这样做)。 You probably meant, without defining it outside your class declaration. 您可能想说的是,没有在类声明之外定义它。 Again the answer is no. 同样,答案是否定的。
You have to separate declaration and definition for this case (it only works with primitive integral types like int to initialize these directly in the class declaration). 在这种情况下,您必须将声明和定义分开(它只能与基本整数类型(如int一起使用)直接在类声明中进行初始化)。

First have a simple declaration in your class declaration (usually something like CData.hpp ) 首先在类声明中有一个简单的声明(通常是CData.hpp类的东西)

namespace misc {
    class CData {
    public:
        CData( ) { };
        CData( int d );

        CData& operator = ( const CData& d );

        static const CData& FIRST;

    private:
        int data;
    };
}

and then define it in a separate compilation unit (usually something like CData.cpp ) 然后在一个单独的编译单元中定义它(通常是CData.cpp类的CData.cpp

namespace misc {
    const CData& CData::FIRST = CData( 512 );
}

... without the need to declare it somewhere in the scope. ...无需在范围内的某个地方声明它。 Is that by any chance possible? 那有可能吗?

No. 没有。

C++ Standard n3337 § 9.4.2/2 C ++标准n3337§9.4.2 / 2

Static data members 静态数据成员

The declaration of a static data member in its class definition is not a definition and may be of an incomplete type other than cv-qualified void. 静态数据成员在其类定义中的声明不是定义,并且可以是cv限定的void以外的不完整类型。 The definition for a static data member shall appear in a namespace scope enclosing the member's class definition. 静态数据成员的定义应出现在包含该成员的类定义的名称空间范围中。 (...) (...)

You can declare a static data member in class: 您可以在类中声明一个静态数据成员:

namespace misc {
    class CData {
    public:
        //...
        static const CData FIRST;  // declaration
        //...
}

and define it in (exactly) one of the .cpp files: 并在(完全).cpp文件之一中进行定义:

namespace misc {
    CData CData::FIRST = CData( 512 );  // definition
}

This is preferred solution, however you need to have this definition out of your class. 这是首选的解决方案,但是您需要在类之外使用此定义。 You could have defined the member in class if it was of an integral type 如果它是整数类型,则可以在类中定义该成员

C++ Standard n3337 § 9.4.2/3 says C ++标准n3337§9.4.2 / 3说:

If a non-volatile const static data member is of integral or enumeration type, its declaration in the class definition can specify a brace-or-equal-initializer in which every initializer-clause that is an assignment- expression is a constant expression (...) 如果非易失性const静态数据成员是整数或枚举类型,则其在类定义中的声明可以指定大括号或相等初始化器,其中每个作为赋值表达式的初始化器子句都是一个常数表达式(。 ..)

For non-integral data, something like this is preferred since it avoids the static initialization fiasco. 对于非整数数据,首选这样的方法,因为它避免了静态初始化的失败。

static const CData FIRST()
{
    static CData first(512); //only initialized once, when first requested

    return first;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM