Inheritance Initialization Question

Hi,
I'm starting to get the hang of this inheritance thing, but I'm completely thrown off by the order of which initialization events take place. I'm wondering if I'm missing something blatantly obvious! Here is an example of something that is completely different than I expected. Suppose I have a couple of classes:
1
2
3
4
5
6
7
8
9
10
11
class A
{
     A(string _a,double _b,double _c,double _d):a(_a),b(_b),c(_c),d(_d)
        {
          cerr<<"A's "<<a<<"="<<b*c*d;
        }
     string a;
     double b;
     double c;
     double d;
};

and
1
2
3
4
5
6
7
8
9
10
11
class B
{
     B(A _a):a(_a.a),b(_a.b),c(_a.c),d(_a.d)
        {
          cerr<<"B's "<<a<<"="<<b*c*d;
        }
     string a;
     double b;
     double c;
     double d;
};

Then suppose I want to inherit A and B:
1
2
3
4
5
6
7
8
9
10
11
12
class C:public A
{
     C(string _a,double _b,double _c,double _d,double _e):A(_a,_b,_c,_d),e(_e)
        {
          cerr<<"C's "<<a<<"="<<b*c*d*e;
        }
     string a;
     double b;
     double c;
     double d;
     double e;
};

and
1
2
3
4
5
6
7
8
9
10
11
12
13
class D: public B
{
     D(C _c):B(_c),e(_c.e)
        {
          _c.b*=2.0;//somehow before getting passed to old B constructor
          cerr<<"D's "<<a<<"="<<b*c*d*e;
        }
     string a;
     double b;
     double c;
     double d;
     double e;
};

Is there some way to do that type of thing?
I mean, I have two questions really. I seem to always end up having inherited constructors called *before* my new constructor code when I want to have the old constructor code called after my new constructor section. Can the old constructor get called after new code? If so, how? Also, I seem to be having trouble passing inherited classes into inherited classes as illustrated above--at least I haven't got that type of thing to work. Is this possible, and if so, how?
Thanks,
Sean
Base classes are *always* constructed before derived; you cannot change that behavior.
I think there might be some ambiguity with all of the repeated members in the hierarchy...you may not be initializing what you are printing.
Ok, if Base classes are *always* constructed before derived classes, is there then no way to use the principles of inheritance if all that needs to be inherited is everything at the ends of a program? Maybe I'm missing something, but it seems to throw away what I thought was the whole point of re-usability of code if there is no way to call an already written constructor after newly added code. I'm so confused suddenly. Is this ruining the whole language for everyone else too? Or if not, what am I missing?
+1 mooerecm

Someone else had this same problem recently. See this thread:

http://www.cplusplus.com/forum/general/36001/#msg194899
Base classes have to be initialized first. The theory is that the derived object, being a "base plus stuff", needs to have
the "lower level" stuff initialized first. Sort of like having to build the first floor of the building before building the
second floor.
Topic archived. No new replies allowed.