A problem about function definition

Nov 21, 2008 at 8:16am
please solve it for me ,expert:


class CSourceLine {
public:
CSourceLine( unsigned int lineNr ) : m_lineNr( lineNr )
{
........ ;
}
unsigned int m_lineNr;
.....
}

refer to the code above, what is the meaning of the statement "m_lineNr( lineNr ) "??
thanks in adv
Nov 21, 2008 at 10:13am
This is a constructor inicialization list.

This means:

1
2
3
4
CSourceLine( unsigned int lineNr ) 
{
        m_lineNr = lineNr;
}


The most important using of it is initialization const vars.
Nov 21, 2008 at 1:23pm
The preferred way to initialize all class members in a constructor is via initializer list. There is never a case where explicit assignment in the body is better. The latter should be used only when it isn't feasible/practical to initialize in the initializer list.
Nov 21, 2008 at 1:43pm
The reason for the initialization list being preferred over assignment is because the members can be initialized or constructed with their value once. Using assignment in a constructor means that the members are initialized or constructed with default values and then reassigned new values, so it's almost like doing it twice.
Topic archived. No new replies allowed.