Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Sequence of same rank constructor call

Reply
Thread Tools

Sequence of same rank constructor call

 
 
francesco.orsenigo@gmail.com
Guest
Posts: n/a
 
      08-15-2006
In the code below, at instantiation of a "listC" object, the
constructors for attributes "a", "b", and "c" are called in sequence
from "a" to "c".
How much is this behaviour reliable?
Does the c++ specifics tell something about it?
What are the odds for the constructors to be called in a different
sequence, like "cba" or "bca" or whatever?
What if "a", "b" and "c" are not of the same class?

Thanks,
Francesco



#include <iostream>

class itemC { public:
int item;
itemC() { std::cout << "init object at " << (void*)this <<
std::endl; }
};

class listC { public:
itemC a;
itemC b;
itemC c;
};

int main()
{
listC list;
}

//EOF

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      08-15-2006
(E-Mail Removed) wrote:
> In the code below, at instantiation of a "listC" object, the
> constructors for attributes "a", "b", and "c" are called in sequence
> from "a" to "c".
> How much is this behaviour reliable?
> Does the c++ specifics tell something about it?
> What are the odds for the constructors to be called in a different
> sequence, like "cba" or "bca" or whatever?
> What if "a", "b" and "c" are not of the same class?
>
> Thanks,
> Francesco
>
>
>
> #include <iostream>
>
> class itemC { public:
> int item;
> itemC() { std::cout << "init object at " << (void*)this <<
> std::endl; }
> };
>
> class listC { public:
> itemC a;
> itemC b;
> itemC c;
> };
>
> int main()
> {
> listC list;
> }
>
> //EOF


The constructors are guaranteed to be called in the order of member
declaration. Even if you put their constructors in a different order in
your initialization list, they will still be called in the order of
member declaration.

Cheers! --M

 
Reply With Quote
 
 
 
 
francesco.orsenigo@gmail.com
Guest
Posts: n/a
 
      08-15-2006

Wow, that was fast!
Thanks man, this saves me a lot of troubles!!!!

Cheers,
Francesco

> The constructors are guaranteed to be called in the order of member
> declaration. Even if you put their constructors in a different order in
> your initialization list, they will still be called in the order of
> member declaration.
>
> Cheers! --M


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
How to call default constructor from other constructor of the same class? romayankin@gmail.com C++ 9 03-06-2006 09:30 PM
How to call a constructor from a constructor PengYu.UT@gmail.com C++ 10 05-09-2005 10:05 AM
Constructor call constructor lallous C++ 3 03-05-2004 11:09 AM
call null constructor from another constructor Matt Graham C++ 3 02-07-2004 08:41 AM



Advertisments