Does in class member initialization takes place at compile time or run-time?

前端 未结 5 1108
醉话见心
醉话见心 2021-02-02 09:42

In C++11 a new feature was introduced where the programmer can initialize class member variables inside class\'s definition, see code below:

struct foo
{ 
  int         


        
5条回答
  •  抹茶落季
    2021-02-02 10:16

    In-class initialisers for member-variables are syntactic sugar for writing them in the constructor initialiser list, unless there's an explicit initialiser already there, in which case they are ignored.
    In-class initialisers of static const members are for constant literals, a definition is still needed (though without initialiser).

    C++ has the "as if"-rule from C, so anything resulting in the prescribed observed behavior is allowed.
    Specifically, that means static objects may be initialised at compile-time.

提交回复
热议问题