R
Rahul
Hi Everyone,
I was trying to understand the constructor order execution and i
tried the following example,
#include <stdio.h>
class A
{
public: A()
{
printf("A\n");
}
};
class D
{
public : D()
{
printf("D\n");
}
D(int i)
{
printf("D is %d\n",i);
}
};
class E
{
public: E()
{
printf("E\n");
}
E(int i)
{
printf("E is %d\n",i);
}
};
class C : virtual public E, virtual public D
{
public: C()
{
printf("C\n");
}
C(int a) : D(a), E(a)
{
printf("C is %d\n",a);
}
};
class B : public A, public C
{
C c_obj;
public: B() : C(5), c_obj(10)
{
printf("B\n");
}
};
int main()
{
B obj;
return (0);
}
I get the following output,
E // virtual base (left to right)
D // virtual base (left to right)
A // base class
C is 5 // base class
E is 10 // member object
D is 10 // member object
C is 10 // member object
B // constructor
Yes, the virtual base class constructors (default) are invoked first
for obj as expected.
However, for the member object c_obj, the virtual base class
constructors aren't invoked ( i mean the default constructor of E and
D) instead the second version ( accepting an integer ) is called?
Is there any special rule for member objects as far as initialization
of virtual base classes are concerned?
I was trying to understand the constructor order execution and i
tried the following example,
#include <stdio.h>
class A
{
public: A()
{
printf("A\n");
}
};
class D
{
public : D()
{
printf("D\n");
}
D(int i)
{
printf("D is %d\n",i);
}
};
class E
{
public: E()
{
printf("E\n");
}
E(int i)
{
printf("E is %d\n",i);
}
};
class C : virtual public E, virtual public D
{
public: C()
{
printf("C\n");
}
C(int a) : D(a), E(a)
{
printf("C is %d\n",a);
}
};
class B : public A, public C
{
C c_obj;
public: B() : C(5), c_obj(10)
{
printf("B\n");
}
};
int main()
{
B obj;
return (0);
}
I get the following output,
E // virtual base (left to right)
D // virtual base (left to right)
A // base class
C is 5 // base class
E is 10 // member object
D is 10 // member object
C is 10 // member object
B // constructor
Yes, the virtual base class constructors (default) are invoked first
for obj as expected.
However, for the member object c_obj, the virtual base class
constructors aren't invoked ( i mean the default constructor of E and
D) instead the second version ( accepting an integer ) is called?
Is there any special rule for member objects as far as initialization
of virtual base classes are concerned?