Hi All,
i am curious to know how to set the value (value=10) through B's constructor in below programme. below is example of multiple inheritance. As construction always happens from right to left so it always call the 'C' class's constructor and set the value 20 but
as per my requirement if i want to call only B class's constructor and set the value 10 it is not happening. so how does this virtual inheritance solve this problem here.

#include<iostream>
using namespace std;

class A
{
int x;
public:
void setX(int i) {x = i;}
void print() { cout << x; }
};

class B: virtual public A
{
public:
B() { setX(10); }
};

class C: virtual public A 
{
public:
C() { setX(20); }
};

class D: public B, public C {
};

int main()
{
    D d;

    d.print();

    return 0;
}

Recommended Answers

All 9 Replies

Is there a reason you can't reverse the order of the inheritence?:

class D : public C , public B {
};

I don't want to change the order as i need to set these values from both B and C classes.
without changing the order of inheritance i should be able to print the both sets of the values.

class D : public C , public B {
};// It will give me only value set under B not in C
class D : public B , public C {
};//it will give me only value set under C not in B

Your question doesn't seem to make sense.

You have one variable x that is defined in A and inherited in all its subclasses.
That variable can only have one value at any one time.
The constructors set x variously, and the value of x will be the last value set in any constructor.
When you create an instance of a class all its superclass constructors are called before the subclass cobstructor is executed - this is fundamental to how inheritance works.
After creating the instance, x will have a value that you can print.

What do you imagine happening when you ask "i should be able to print the both sets of the values". Where do you imagine the other other values will be?

my purpose is here to set diff-2 values in diff-2 context i.e. (B->10 && C->20) and anytime want to access these values through D as both classes (B&C) is having different context to set the value. Please correct me if it can be achieved through changing this design.

OK. This is pushing the limits of my C++ knowledge (I despise C++ ... should have been strangled at birth) but:
You need B and C each to have their own variables, ie not inherited from A.
I know that for functions you can declare a virtual function in A, and implement it in both B and C which will work exactly as you describe, but as far as I know there's no virtual equivalent for variables. If so you can wrap the variables in accessor methods that are defined virtual in A.

There are probably solutions involving casting pointers to the classes, but that way madness lies.

yes it is required there , will take extra variable with get method to access the value set in constructor of B and C.

I despise C++ ... should have been strangled at birth

Blasphemy.

You have one variable x that is defined in A and inherited in all its subclasses.
That variable can only have one value at any one time.

Yep.

I'm seeing a mix of singular ("value") and plural ("values") in the posts. There is ONE variable x that can hold ONE value. Thus it can hold 10 or it can hold 20, but it cannot hold both. No change in the CODE can alter the fact that there is space set aside for one integer if x is declared in A. If you need two variables called x, one in B and one in C, you should declare x in B and x in C and not in A. Then the x in B can hold 10 and the x in C can hold 20 simultaneously because there is space for two variables, not one.

commented: QuantumC++ would have to be used to hold all values and answers but that compiler is not out yet. +11

You could also declare instances of each instead of trying to inherit from them:

class A
{
    int x;
public:
    void setX( int i ) {
        x = i;
    }
    void print() {
        cout << x;
    }
};
class B : virtual public A
{
public:
    B() {
        setX( 10 );
    }
};
class C : virtual public A
{
public:
    C() {
        setX( 20 );
    }
};
class D {
public:
    B bItem;
    C cItem;
    void PrintB()
    {
        bItem.print();
    }
    void PrintC()
    {
        cItem.print();
    }
};

Thanks all for your efforts and sharing the valuable informations.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.