non-integral constants

前端 未结 5 1628
-上瘾入骨i
-上瘾入骨i 2020-12-28 08:39

I want a header file with a non-integral constant in it, e.g. a class. Note the constant does not need to be a compile-time constant.

stati         


        
5条回答
  •  暖寄归人
    2020-12-28 09:25

    I think the other answers here are better, but if you're dead-set on doing it all with headers, you can effectively inline your object (as you specifically ask) with a simple wrapper function.

    inline const std::string &get_ten() {
        static const std::string ten = "10";
        return ten;
    }
    

    There will be only one string, initialized once, and you don't need anything outside of the header file.

提交回复
热议问题