I’m running into a strange error when a constructor is called for one of my classes. Essentially, what I’m doing is this:
I have a class “A”, that has two member variables of type “B”, “C”. “C” has to be initiated with a member of type “B”. So the constructor has to be:
A::A():
c(b)
{}
This works fine if the class is laid out as:
class A
{
B b;
C c;
}
But crashes if the class is laid out as:
class A
{
C c;
B b;
}
My first guess was that, of course b has to be created before c if I’m going to
initialize c(b) in the constructor. I don’t know if this is correct though. Is
the constructor called before any member variables are allocated? Or are the
member variables referenced in the constructor allocated first and then any remaining
unreferenced member variables allocated at the end of the constructor (like for example if there was
another member variable “C c2” that is unreferenced in the constructor)?
I am on Visual Studio 2010.
So, the way this works is that:
I could be mistaken about the placement of #3, I rarely use virtual bases and rarely write code that depends on this stuff. Why? Because it’s complicated and such code is extremely fragile.
Destruction happens in exactly the opposite order.