How to define a const double inside a class's header file?

后端 未结 3 699
滥情空心
滥情空心 2021-01-02 02:54

Inside the header file of my class, I am trying the following and getting compiler complaints:

private:
    static const double some_double= 1.0;


        
相关标签:
3条回答
  • 2021-01-02 03:29

    I've worked around this issue by doing this:

    //my_class.hpp
    const double my_double() const {return 0.12345;}
    
    //in use
    double some_double = my_class::my_double();
    

    I got the idea from

    math::pi()
    
    0 讨论(0)
  • 2021-01-02 03:47

    Declare it in the header, and initialize it in one compilation unit (the .cpp for the class is sensible).

    //my_class.hpp
    private:
    static const double some_double;
    
    //my_class.cpp
    const double my_class::some_double = 1.0;
    
    0 讨论(0)
  • 2021-01-02 03:49

    In C++11, you can have non-integral constant expressions thanks to constexpr:

    private:
        static constexpr double some_double = 1.0;
    
    0 讨论(0)
提交回复
热议问题