Inheritance with Virtual

Hello everyone.

I've wrote this minimal example :
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
36
37
38
39
#include <iostream>
#include <cassert>

static const unsigned int defData(0xff);

class CBasedata
{
public:
    CBasedata(unsigned int data=defData) : mydata(data) {}; 
    virtual ~CBasedata(){};
protected:
    unsigned int mydata;
};

class CBase : virtual public CBasedata
{
public:
    CBase(unsigned int data=defData): CBasedata(data) {};
    virtual ~CBase(){};
    unsigned int data(){ return mydata; };
};


class CDerived : virtual public CBase
{
public:
    CDerived(unsigned int data) : CBase(data) {}; 
     virtual ~CDerived(){};
};

int main ( int argc, char** argv)
{
    const unsigned int derivedValue(1);
    CDerived myDerived(derivedValue); 

    std::cerr << "My original data is " << derivedValue << "\n";
    std::cerr << "My derived  data is " << myDerived.data() << "\n";
    assert(derivedValue == myDerived.data());
};


which gives me the following output:

My original data is 1
My derived  data is 255
CMinimalInheritance.exe: CMinimalInheritance.cpp:38: int main(int, char**): Assertion `derivedValue == myDerived.data()' failed.


The program works fine without the virtual qualifiers or when i call teh CBasedata constructor in CDerived constructor explicitly. So the CBase constructor doesn't seem to call the CBasedata constructor in the virtual case.

Does enybody know, if this is intended, or maybe a compiler bug?

Thanks in advance for answers.
Last edited on
It should be CDerived(unsigned int data) : CBasedata(data) {}

See why here -> http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.12

And you don't need the virtual keyword here -> class CDerived : virtual public CBase
Thanks for Clarification!

I figured it might have to do something with the virtual inheritance, but was not quite shure.

And yes, I need the virtual keyword in class CDerived : virtual public CBase because the CBase is the multiple inherited class.

But I might don't need the virtual keyword in class CBase : virtual public CBasedata.


Regards!
I think you dont need virtual keyword at all . virtual keyword is only to avoid dimond inheritance problem .
bluecoder wrote:
virtual keyword is only to avoid dimond inheritance problem

Yes, when used like this. I thought the OP was aware of that and what he posted was just part of his code (the part where the problem was located). Anyway, in the same FAQ I gave him above he can also see what's the diamond problem and how he can use the virtual keyword to solve it.

http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.8
http://www.parashift.com/c++-faq-lite/multiple-inheritance.html#faq-25.9
Last edited on
Oh, I didnt see the latest answers.

"m4ster r0shi" is right in the assumption, that I posted only a part of the code. As is wrote, I have multiple inherited classes from CBase. It's like this:
1
2
class CNiceFeature : public virtual CDerived;
class CDerivedWithNiceFeature:  public virtual CNiceFeature, public virtual CDerived

CNiceFeature is derived from CBase because it need access to CBaseData and CBase members and has additional parameters. Maybe there is a better way to do this. There are a lot of classes derived from CBase and CNiceFeature.

As the FAQ clarified (thanks again), I need to state all constructor manually on virtual derived classes. Otherwise the default constructor is called.

I guess my initial example was wrong in that way, that it mislead to a wrong assumption about the intended structure. Will do better next time.

Last edited on
Topic archived. No new replies allowed.