Correct place to initialize class variables?

前端 未结 5 1960
盖世英雄少女心
盖世英雄少女心 2021-01-31 04:28

Where is the correct place to initialize a class data member? I have the class declaration in a header file like this:

Foo.h:

class Foo {
private:
    in         


        
5条回答
  •  Happy的楠姐
    2021-01-31 04:57

    It can be initialized directly in the header file, in c++11 or gnu++11:

    int myInt = 1;
    

    See this article "C++11 Tidbits: Non-static Data Member Initializers"

提交回复
热议问题