Monday, November 13, 2006

C++ : Is Member Initialization List Efficient ?

Yes it is, and should be made a habit. Consider the following example:

class Anjelina {

public:
// very naive ....
Anjelina( ) {
_name = 0; //_name is NULL now
_blemish = 0;
}

private:
String _name;
int _blemish;
};


What happens before the start of constructor call?

An object is fully constructed. What !!! Yes, it is. Before the constructor call, the 'this' pointer is initialized and all the members are constructed (their default constructors called). The member constructor is called if it is non-trivial. So, what's wrong in the above constructor? Nothing, it's just naive. The reason is, that the constructor of _name is already called and String already initialized to 0 (default behavior for String object). The code transformation that takes place is:

// Pseudo C++ Code
Angelina::Angelina (/* 'this' pointer goes here */) {
// invoke default String constructor
_name.String::String ( ) ;
// generate temporary
String temp = String ( 0 ) ;
// memberwise copy _name
_name.String::operator= ( temp ) ;
// destroy temporary
_name.String::~String( );

_blemish = 0 ;
}


So the proper code for constructor should be:

Anjelina( ):_name(0) {
_blemish = 0;
}

or even better (non-confusing code) ...

Anjelina( ):_name(0), _blemish(0) { }

This will call the constructor with 0 as an argument. If you want String's default constructor to be called, you can write _name( ).

Please click on the ads to your right :p

5 comments:

Anonymous said...

If we use a member initialization list to initialize members of a class, then can it be that we initialize some members, but not all?

//Example: only relevant code.
class A
{
public:
A(int d1);

private:
int data1;
int data2;
}

// Implementation

A::A(int d1):data1(d1)
{
data2 = 10;
}
/*******************************/

That is, data1 is initilazed in the list, whereas data2 in the ctor body. Is this allowed? Or, we have to do all initializations either in the ctor body, or in the list?

Zaman Bakshi said...

Yes you very much can. In fact, you can do this too:

A::A(int d1):data1(d1),data2(10){
data1=100;
data2=-10; //etc
}

Unknown said...

Thanks, I now know that An object is fully constructed! Great code too!

infomercial services

Anonymous said...

My introduction
Hi all, a neighbor just advised myself about this online community I really thought i will come and consider a search and present myself, Seems to be a fantastic site with a lot of people!

Anonymous said...

[url=http://www.nikefreeshoeshop.com]nike free 5.0[/url]

tonight, I'm a person's... "Even the China enchanting enchanting relying on guys, care-free whisper Route that will night time, it is the girl wedding night, might be subsequent the particular girlfriend for you to send the woman's something special withher fiance lurking Pornographic stepmother routed her product would be to airport transfer all your girlfriend possessions, in addition to premeditated killing of the girl life; of which nighttime, your lover ended up being fleeing compelled partnerships indulgence, the icon Lieyan, the end on the 100 % pure body on the midday greedyHuan, even the Chinese language wave from the sleeves, converted as well as boarded a airplane destined to get Portugal. All 5 many years later, as the us president connected with FL, and even Tiongkok holding a awesome large full-old little boy moved off the aircraft, this time around, she will get rid of any 1000 periods settle vengeance, on the curtain, baby looking eyeing, all of the chief executive: "You have got to participate in addition to my own mummy? Ganqiang I'm a lady, are lifeless!
[url=http://www.nikefreeshoeshop.com]nike free run 3.0[/url]