Another inheritance question

Hi everyone!
can you please explain me WHY in the C class,the line "B _b1;"
doesnt activate the B deafault CTOR?

and i will be glad to get some advices on those codes, i can not answer them correctly, a lot of mistakes..
when do the derived CTOR is called? and so on.. THANKS !

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
 class A 
{
public:
	A()			{cout<<"(1)"<<endl;}
	A(A& a)		{cout<<"(2)"<<endl;}
	virtual void f()  {cout<<"(3)"<<endl;}	
	virtual A g() {h(); cout<<"(4)"<<endl; return *this;}
	virtual A& h(){A::f(); cout<<"(5)"<<endl; return *this;}
	~A()	   {cout<<"(6)"<<endl;}
};

class B : public A
{
	int _x;
	A _a1;
	A* _ptr;
   public:
	B():_x(0),_ptr(0)	{cout<<"(7)"<<endl;}
	B(int num):_x(num),_ptr(0)	{cout<<"(8)"<<endl;}
	virtual void f()	      {g();cout<<"(10)"<<endl;}
	A g()		      {cout<<"(11)"<<endl; return A::g();}
 	B k()		           {cout<<"(12)"<<endl; return *this;}
    ~B()	  		      {cout<<"(13)"<<endl;}
};


class C : public B, public A
{
	B _b1;
   public:
	C():_b1(0)	{cout<<"(14)"<<endl;}
	A g()		{cout<<"(15)"<<endl; B::k(); return *this;}
	virtual void k()	{cout<<"(16)"<<endl;}
	virtual ~C()	{cout<<"(17)"<<endl;}
};
line 31: _b1 is constructed using B's constructor that accepts an int (line 19).
but why not with the default CTOR? in the private section (line 29) it already declared..
Because you used the initialization syntax
 
:_b1(0) 

That overrides any use of the default constructor. You stated you wanted the int constructor. You can't instantiate an object (_b1) with two different constructors (default and int).

Topic archived. No new replies allowed.