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)
Initialization lists allow you to choose which constructor is called and what arguments that constructor receives. If you have a reference or a const field, or if one of the classes used does not have a default constructor, you must use an initialization list.
The most common benefit of doing this is improved performance. If the expression whatever is the same type as member variable x_, the result of the whatever expression is constructed directly inside x_ — the compiler does not make a separate copy of the object.
The initializer list is used to directly initialize data members of a class. An initializer list starts after the constructor name and its parameters.
Initialization lists. In fact, constructors should initialize as a rule all member objects in the initialization list.
m_somemap
std::map< std::string, std::string >
, i.e., a valid instance of that map which has no elements in it.m_somebool
true
or false
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.m_someint
There is no need to actually do it.
The default constructor will autmatically do it.
But somtimes by making it explicit it acts as sort of documentation:
class X
{
std::map<string,string> data;
Y somePropertyOfdata;
X()
:data() // Technically not needed
,somePropertyOfdata(data) // But it documents that data is finished construction
{} // before it is used here.
};
The rule in C++ is that unless you explicitly initialise POD data it is undefined while other classes have there default constructor called automatically (even if not explicitly done so by the programmer).
But saying that. Consider this:
template<typename T>
class Z
{
T data;
Z()
:data() // Technicall not need as default constructor will
// always be called for classes.
// But doing this will initialize POD data correctly
// if T is a basic POD type.
{}
};
Here you would exepect data to be default initialized.
Technically POD do not have constructors so if T was int then would you expect it to do anything? Becuase it was explicitly initialize it is set to 0 or the equivalent for POD types.
For the edit:
class A
{
std::map<string,string> m_someMap;
int m_someint;
bool m_somebool;
public:
A::A()
: m_someMap() // Class will always be initialised (so optional)
, m_someint(0) // without this POD will be undefined
, m_somebool(false)// without this POD will be undefined
{}
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With