Please explain how to use member initialization lists.
I have a class declared in a .h
file and a .cpp
file like this:
class Example
{
private:
int m_top;
const int m_size;
...
public:
Example ( int size, int grow_by = 1 ) : m_size(5), m_top(-1);
...
~Example();
};
I'm initializing m_size
on object creation because of const
. How should I write the constructor?
Should I repeat : m_size(5), m_top(-1)
, or I can omit this step?
Example::Example( int size, int grow_by)
{
... some code here
}
or
Example::Example( int size, int grow_by) : m_size(5), m_top(-1)
{
... some code here
}
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…