为什么整数成员变量(这不是量静态)初始化不是在C ++允许吗? [英] Why is initialization of integer member variable (which is not const static) not allowed in C++?

查看:165
本文介绍了为什么整数成员变量(这不是量静态)初始化不是在C ++允许吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试初始化类定义INT成员变量我的C ++编译器抱怨。它告诉只有静态常量整型数据成员可以在类中被初始化。能否请您解释这个限制的理由(如果可能的例子)。

My C++ compiler complains when i try to initialize a int member variable in class definition. It tells "only static const integral data members can be initialized within a class". Can you please explain the rationale behind this restriction (if possible with example).

推荐答案

的理由是C的低层次的性质++。如果允许,编译器需要生成初始化code的所有构造函数是不完全清楚的开发商。

The rationale is the "low-level" nature of C++. If it would allow this, the compiler would need to generate initialization code for all constructors which is not entirely clear to the developer.

毕竟,可能有必要进行初始化基类成员即使基类的构造函数没有显式调用派生类的建设。

After all it might be necessary to initialize members of base classes on the construction of a derived class even when the base class constructors are not explicitly invoked.

静态常量组成变量不需要intitalization在创建对象。

Static const integral variables do not need intitalization upon object creation.

这篇关于为什么整数成员变量(这不是量静态)初始化不是在C ++允许吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆