简体   繁体   English

如何在C ++中的构造函数中定义extern const

[英]How to define an extern const in constructor in C++

Two config.json files hold values that I want to store into constants. 两个config.json文件包含我想要存储到常量中的值。 My plan was to load and parse the json file in the constructor of the corresponding classes and assign the values to constants. 我的计划是在相应类的构造函数中加载和解析json文件,并将值分配给常量。 However, in debugging mode, the error " ... LNK2019: unresolved external symbol..." occurs. 但是,在调试模式下,会出现错误“... LNK2019:未解析的外部符号...”。

So, is it not possible to define constants in a constructor? 那么,是不是可以在构造函数中定义常量?

Since the values of the constants depend on the variable VARIANT , I've made a ifelse to read the correct .json file. 由于常量的值取决于变量VARIANT ,我已经做了一个ifelse来读取正确的ifelse文件。

// constants.h
extern const int    BARL;
extern const int BAR_TOL;

// constants.cpp
Constants::Constants() {
    Json::Reader reader;
    Json::Value root;

    #if VARIANT == A
        std::ifstream config_a_file("a.json");
        reader.parse(config_a_file, root);
    #elif VARIANT == B
        std::ifstream config_b_file("b.json");
        reader.parse(config_b_file, root);
    #endif

    const int   BARL = root["BARL"].asInt();
    const int   BAR_TOL = BARL * 3;
}

What I expect is that the constants BARL and BAR_TOL can be used in the entire constants.cpp class. 我期望的是常量BARLBAR_TOL可以在整个constants.cpp类中使用。 However, their definition cannot be found. 但是,他们的定义无法找到。

When declaring external variables, the scope they appear in must match the scope they are defined in, to the namespace level. 声明外部变量时,它们出现的范围必须与它们在其中定义的范围相匹配,才能与命名空间级别相匹配。 And you can't define two globals with external linkage in block scope at all. 并且你根本无法在块范围内定义两个具有外部链接的全局变量。

Putting aside the discussion 1 about why you should be careful with such global constants, I'd say you are going about this wrong. 暂且不论为什么你要小心这样的全局常量的讨论1,我说你要对这个错误的。 Since you have a Constants class, just expose those constants as members, and declare a single external Constants instance. 由于您有一个Constants类,只需将这些常量公开为成员,并声明一个外部Constants实例。 Something like: 就像是:

struct Constants {
    int    BARL;
    int BAR_TOL;
    Constants();
};

extern Constants const constants;

Then other code may deal with the better namespaced constants.BARL , which is itself a constant since it's a sub-object of the const constants object. 然后其他代码可以处理更好的命名空间constants.BARL ,它本身是一个常量,因为它是const constants对象的子对象。

The corresponding cpp file then becomes this: 然后相应的cpp文件变为:

Constants const constants; // definition

Constants::Constants() {
    Json::Reader reader;
    Json::Value root;

    #if VARIANT == A
        std::ifstream config_a_file("a.json");
        reader.parse(config_a_file, root);
    #elif VARIANT == B
        std::ifstream config_b_file("b.json");
        reader.parse(config_b_file, root);
    #endif

    BARL = root["BARL"].asInt(); // assignment
    BAR_TOL = BARL * 3;
}

1 - For that, you can browse for many examples about why the unspecified initialization order between static variables in different translation units can cause headaches. 1 - 为此,您可以浏览以获取有关为什么不同转换单元中的静态变量之间未指定的初始化顺序可能导致令人头疼的示例。

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

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