将静态变量对象初始化为相同的值

时间:2018-03-29 07:14:30

标签: c++ static initialization

我想将所有静态变量对象初始化为相同的值。我在prog1.h

中定义了一个类
  namespace fal {
    class read_f{
    public:
        static std::string ref_content, seq_content;
        int read_fasta(int argc, char **argv);
    };
}

我尝试在prog1.cpp

中初始化它们
std::string fal::read_f::ref_content = seq_content = "";

但我得undefined reference error

当我尝试

std::string fal::read_f::ref_content = "" ;
std::string fal::read_f::seq_content = "";

它工作正常。

如何在一行中初始化?

1 个答案:

答案 0 :(得分:3)

您用逗号内联它们。如果您不想重复fal::限定符,可以使用using声明:

using fal::read_f;
std::string read_f::ref_content = "", read_f::seq_content = "";

此外,从C ++ 17开始,您可以创建这两个变量inline,以便它们可以在类定义中定义([class.static.data])。 / p>