简体   繁体   English

如何在成员初始值设定项列表之外初始化类成员

[英]How to initialize class member outside the member initializer list

I have a C++ class that takes a string (file path) in its constructor, loads that file into a JSON and then uses some of the JSON variables to initialize another member.我有一个 C++ 类,它在其构造函数中接受一个字符串(文件路径),将该文件加载到 JSON 中,然后使用一些 JSON 变量来初始化另一个成员。

As the file needs to be loaded first, and the the JSON initialized (from stream), I cannot initialize thing in the member initializer list.由于文件需要先加载,并且 JSON 初始化(来自流),我无法初始化成员初始化列表中的thing Should I use another wrapper class for the JSON, use new...?我应该为 JSON 使用另一个包装类,使用新的...? How can I achieve this?我怎样才能做到这一点?

class Dummy
{
    std::string _configFilePath;
    json configJson;
    Thing thing;

    Dummy(std::string configFilePath = "../config.json") :
    _configFilePath(configFilePath)
    {
        std::ifstream ifs(configFilePath);
        ifs >> configJson;
        thing(configJson["whatever"]); // thing can't be initialized here
    }
};

Note that thing is not default constructible.请注意, thing不是默认可构造的。

Is Thing both default constructable and move-assignable? Thing是否既可默认构造又可移动分配? If so:如果是这样的话:

class Dummy
{
    std::string _configFilePath;
    json configJson;
    Thing thing;

    Dummy(std::string configFilePath = "../config.json") :
    _configFilePath(configFilePath)
    {
        std::ifstream ifs(configFilePath);
        ifs >> configJson;
        thing = Thing(configJson["whatever"]);   // move-assign a new object
    }
};

You can use a helper function to do what the current constructor does:您可以使用辅助函数来执行当前构造函数的操作:

class Dummy
{
    std::string _configFilePath;
    json configJson;
    Thing thing;

    Thing loader() {
        std::ifstream ifs(_configFilePath);
        ifs >> configJson;
        return Thing(configJson["whatever"]);
    }

    Dummy(std::string configFilePath = "../config.json") :
        _configFilePath(configFilePath), thing(loader())
    {
    }
};

This will construct _configFilePath and default construct configJson , then call loader to load the thing.这将构造_configFilePath和默认构造configJson ,然后调用loader来加载事物。 RVO should enable construction of thing directly in loader . RVO 应该能够直接在loader构建thing

You can use the combination of a delegating constructor and a helper function that loads the json object to properly initilize the objects.您可以结合使用委托构造函数和加载json对象的辅助函数来正确初始化对象。

class Dummy
{
    std::string _configFilePath;
    json _configJson;
    Thing _thing;

    // Use a delegating constructor
    Dummy(std::string configFilePath = "../config.json") :
       Dummy(configFilePath, getConfig(configFilePath) {}

    Dummy(std::string configFilePath, json configJson) :
       _configFilePath(configFilePath),
       _configJson(configJson),
       _thing(configJson["whatever"]) {}

    // Load and return the json object.
    static json getConfig(std::string configFilePath)
    {
       std::ifstream ifs(configFilePath);
       json configJson;
       ifs >> configJson;
       return configJson;
    }
};

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

相关问题 在类成员初始化器列表中初始化向量的元组 - Initialize a tuple of vectors in a class member initializer list 在类的成员初始化器列表中初始化std :: tuple - Initialize std::tuple in member initializer list of a class 如何在C ++类的初始化列表中初始化member-struct? - How to initialize member-struct in initializer list of C++ class? 如何在成员初始值设定项列表中初始化数组 - How to initialize an array in the member initializer list 类成员的唯一指针,带有在初始化程序列表之外的自定义删除器 - class member unique pointer with custom deleter outside initializer list 如何在构造函数初始值设定项列表中初始化std :: unique_ptr对象的类成员std :: vector? - How do I initialize a class member `std::vector` of `std::unique_ptr` object in the constructor initializer list? 如何在初始化列表中初始化普通数组成员变量? - How can I initialize normal array member variable in initializer list? 如何使用 initializer_list 初始化成员数组? - How do I initialize a member array with an initializer_list? 如何使初始化列表和类成员初始化程序一起工作? - How to make initializer list and class member initializer work together? 类成员初始化程序使用错误检查初始化ifstream? - Class member initializer to initialize ifstream with error check?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM