为什么我不能在构造函数中初始化我的静态数据成员

为什么我不能在构造函数中初始化我的静态数据成员

问题描述:

我读了parashift的答案,但我需要一些详细信息,以了解为什么编译器不允许在构造函数中定义静态成员变量。

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.

静态成员变量未与该类的每个对象关联。它由所有对象共享。如果在ctor中初始化,则意味着您正在尝试与类的特定实例关联。由于这是不可能的,因此不允许。

static member variables are not associated with each object of the class. It is shared by all objects. If you initialize in ctor then it means that you are trying to associate with a particular instance of class. Since this is not possible, it is not allowed.