什么时候需要使用初始化器列表初始化C ++类成员? [英] When do I have to use initializer lists for initializing C++ class members?
问题描述
std :: map< std :: string,std :: string> m_someMap
作为A类的私有成员变量 两个问题:(我唯一要问的原因是因为我遇到了代码那个)
-
这一行的目的是什么:
A :: A():m_someMap()
这是初步化的,但你必须这样做吗?
我很困惑。 -
std :: map< std :: string,std :: string> m_someMap
,也是C#定义int,double等始终初始化为defualt 0,对象为null(至少在大多数情况下)
那么C ++中的规则是什么?对象是由defualt初始化为null和原语为垃圾?
当然,我正在使用实例变量。
编辑:
也是,因为大多数人都指出这是一种风格选择,而不是必需的,那就是:
A :: A(): m_someMap(),m_someint(0),m_somebool(false)
m_somemap
- 你不必。
- 如果你省略,你会得到什么:一个空的
的std ::地图< std :: string,std :: string>
,即该地图中没有元素的有效实例。
m_somebool
- 您必须将其初始化为
true
或false
如果您希望具有已知值。布尔是简单的旧数据类型,它们没有构造函数的概念。此外,C ++语言没有为非显式初始化的布尔值指定默认值。 - 如果省略它,您会得到什么:具有未指定值的布尔成员。你不能这样做,后来使用它的价值。因此,您是强制推荐的政策,您可以初始化所有这种类型的值。
m_someint
- 如果您希望具有已知值,则必须将其初始化为一个整数值。整数是普通旧数据类型,它们没有构造函数的概念。此外,C ++语言未指定非显式初始化整数的默认值。
- 如果省略它,你会得到什么:具有未指定值的int成员。你不能这样做,后来使用它的价值。因此,您是一个强烈推荐的政策,您可以初始化所有这种类型的值。
let's say I have
std::map< std::string, std::string > m_someMap
as a private member variable of class A
Two questions: (and the only reason I'm asking is because I came across code like that)
What's the purpose of this line:
A::A() : m_someMap()
Now I know that this is intialization, but do you have to do this like that? I'm confused.
What's the default value of
std::map< std::string, std::string > m_someMap
, also C# defines that int, double, etc. is always initialized to defualt 0 and objects are to null (at least in most cases) So what's the rule in C++?? are object initialized by defualt to null and primitives to garbage? Of course I'm taking about instance variables.
EDIT:
also, since most people pointed out that this is a style choice and not necessary, what about:
A::A() : m_someMap(), m_someint(0), m_somebool(false)
m_somemap
- You don't have to.
- What you get if you omit it: An empty
std::map< std::string, std::string >
, i.e., a valid instance of that map which has no elements in it.
m_somebool
- You have to initialize it to
true
orfalse
if you want it to have a known value. Booleans are "plain old data types" and they do not have the concept of a constructor. Moreover, the C++ language does not specify default values for non-explicitly-initialized booleans. - What you get if you omit it: a boolean member with an unspecified value. You must not do this and later use its value. Because of that, it is a strongly recommended policy that you initialize all values of this type.
m_someint
- You have to initialize it to some integer value if you want it to have a known value. Integers are "plain old data types" and they do not have the concept of a constructor. Moreover, the C++ language does not specify default values for non-explicitly-initialized integers.
- What you get if you omit it: an int member with an unspecified value. You must not do this and later use its value. Because of that, it is a strongly recommended policy that you initialize all values of this type.
这篇关于什么时候需要使用初始化器列表初始化C ++类成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!