本文介绍了为什么我不能在构造函数中初始化我的静态数据成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了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.

这篇关于为什么我不能在构造函数中初始化我的静态数据成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 21:35