在什么地方初始化静态const

时间:2022-09-12 20:04:11

I have a class

我有一个类

class foo {
public:
   foo();
   foo( int );
private:
   static const string s;
};

Where is the best place to initialize the string s in the source file?

初始化源文件中的字符串s的最佳位置在哪里?

5 个解决方案

#1


137  

Anywhere in one compilation unit (usually a .cpp file) would do:

在一个编译单元(通常是.cpp文件)中的任何地方都可以:

foo.h

foo。

class foo {
    static const string s; // Can never be initialized here.
    static const char* cs; // Same with C strings.

    static const int i = 3; // Integral types can be initialized here (*)...
    static const int j; //     ... OR in cpp.
};

foo.cpp

foo.cpp

#include "foo.h"
const string foo::s = "foo string";
const char* foo::cs = "foo C string";
// No definition for i. (*)
const int foo::j = 4;

(*) According to the standards you must define i outside of the class definition (like j is) if it is used in code other than just integral constant expressions. See David's comment below for details.

(*)根据标准,如果在代码中使用i而不只是积分常数表达式,则必须在类定义之外定义i(如j)。详情请见下面大卫的评论。

#2


12  

Static members need to be initialized in a .cpp translation unit at file scope or in the appropriate namespace:

静态成员需要在文件范围的.cpp转换单元中初始化,或者在适当的名称空间中初始化:

const string foo::s( "my foo");

#3


9  

In a translation unit within the same namespace, usually at the top:

在同一名称空间内的翻译单元中,通常在顶部:

// foo.h
struct foo
{
    static const std::string s;
};

// foo.cpp
const std::string foo::s = "thingadongdong"; // this is where it lives

// bar.h
namespace baz
{
    struct bar
    {
        static const float f;
    };
}

// bar.cpp
namespace baz
{
    const float bar::f = 3.1415926535;
}

#4


1  

Only integral values (e.g., static const int ARRAYSIZE) are initialized in header file because they are usually used in class header to define something such as the size of an array. Non-integral values are initialized in implementation file.

头文件中只初始化整数值(例如,静态const int ARRAYSIZE),因为它们通常用于类头来定义数组的大小。在实现文件中初始化非整数值。

#5


-1  

const string foo::s( "my foo");

And it should be initialized in the source file, otherwise it goes wrong when you invoke it in the test case.

并且它应该在源文件中初始化,否则在测试用例中调用时就会出错。

#1


137  

Anywhere in one compilation unit (usually a .cpp file) would do:

在一个编译单元(通常是.cpp文件)中的任何地方都可以:

foo.h

foo。

class foo {
    static const string s; // Can never be initialized here.
    static const char* cs; // Same with C strings.

    static const int i = 3; // Integral types can be initialized here (*)...
    static const int j; //     ... OR in cpp.
};

foo.cpp

foo.cpp

#include "foo.h"
const string foo::s = "foo string";
const char* foo::cs = "foo C string";
// No definition for i. (*)
const int foo::j = 4;

(*) According to the standards you must define i outside of the class definition (like j is) if it is used in code other than just integral constant expressions. See David's comment below for details.

(*)根据标准,如果在代码中使用i而不只是积分常数表达式,则必须在类定义之外定义i(如j)。详情请见下面大卫的评论。

#2


12  

Static members need to be initialized in a .cpp translation unit at file scope or in the appropriate namespace:

静态成员需要在文件范围的.cpp转换单元中初始化,或者在适当的名称空间中初始化:

const string foo::s( "my foo");

#3


9  

In a translation unit within the same namespace, usually at the top:

在同一名称空间内的翻译单元中,通常在顶部:

// foo.h
struct foo
{
    static const std::string s;
};

// foo.cpp
const std::string foo::s = "thingadongdong"; // this is where it lives

// bar.h
namespace baz
{
    struct bar
    {
        static const float f;
    };
}

// bar.cpp
namespace baz
{
    const float bar::f = 3.1415926535;
}

#4


1  

Only integral values (e.g., static const int ARRAYSIZE) are initialized in header file because they are usually used in class header to define something such as the size of an array. Non-integral values are initialized in implementation file.

头文件中只初始化整数值(例如,静态const int ARRAYSIZE),因为它们通常用于类头来定义数组的大小。在实现文件中初始化非整数值。

#5


-1  

const string foo::s( "my foo");

And it should be initialized in the source file, otherwise it goes wrong when you invoke it in the test case.

并且它应该在源文件中初始化,否则在测试用例中调用时就会出错。