Should I define static inline methods in header file?

前端 未结 2 2046
日久生厌
日久生厌 2020-12-12 18:25

I\'ve read about how it is usually best not to define anything in header files because redundant copies are made for every other file that includes the header file. However,

2条回答
  •  误落风尘
    2020-12-12 18:53

    (1) I can't define the static inline method out side of the class definition or at the .cpp file.

    You can define a static inline method outside the class within the header file. Demo. You cannot define them in .cpp file.

    (2) should I bother to use static inline methods at all

    I would say, they can be easily avoided. If you need to see the body in the header file for your own purpose, then only make them inline.

    (3) Should I even define anything method or variable in a header file (what about constants)

    1. Inside the class body, you can define static const data of integral type.
    2. static methods can be defined inside the class body
    3. static inline methods can be defined inside class body or outside the class body within the header file
    4. static data members must be defined in single .cpp file to adhere with One Definition Rule

提交回复
热议问题