Why can't I initialize my static data member in my constructor

后端 未结 5 1941
無奈伤痛
無奈伤痛 2020-12-15 05:28

I read the answer in parashift but I need bit details as to why compiler won\'t allow to define static member variable in constructor.

5条回答
  •  再見小時候
    2020-12-15 06:24

    1) Static variables are property of the class and not the object. 2) Any static variable is initialized before any objects is created.

    • That's why compiler don't allow to define static variable inside constructor as constructor will be called when a object is created.

提交回复
热议问题