Sequence of same rank constructor call

F

francesco.orsenigo

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
 
M

mlimber

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
 
F

francesco.orsenigo

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

Cheers,
Francesco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top