Inheritance constructor problems

I am having trouble executing constructors using parameters in three level inheritance. The following code will explain:

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
class base
{
   public:
   base(int passZ);
   private:
   int z;
};

class subBase: virtual public base
{
   public:
   subBase(int passY int passZ);
   private:
   int y;
   int z;
};

class subSubBase: virtual public subBase
{
   public:
   subSubBase(int passX, int passY, int passZ);
   private:
   int x;
   int y;
   int z;
};


The above are the 'prototypes' the below are the constructor definitions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
base::base(int passZ)
{
   z=passZ;
}
subBase::subBase(int passY, int passZ):base(passZ)
{
   y=passY;
   z=passZ;
}
subSubBase::subSubBase(int passX, int passY, int passZ): subBase(passY, passZ)
{
   x=passX;
   y=passY;
   z=passZ;
}

When I compile it it doesn't seem to work. Any ideas?
Last edited on
What compiler errors are you getting?
it doesn't seem to work

Ah, well since you've described the problem in such helpful detail, it's easy for us to diagnose it.

The problem is that you've done something wrong.

To fix is, you need to do it right.

HTH.
Last edited on
class subBase: virtual public base

i've never seen a class declaration with the virtual keyword in there before. looks weird.
i've never seen a class declaration with the virtual keyword in there before

It's perfectly legal, although it only makes a difference when multiple inheritance is involved.
Last edited on
You need to initialize the virtual base class object. For instance,

1
2
3
4
subSubBase::subSubBase(int passX, int passY, int passZ): base(0), /* added */ subBase(passY, passZ)
{
    // ...
}
The reason why it isn't working is because I used virtual public when I shouldn't have.
Topic archived. No new replies allowed.