0

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;
}
4
Contributors
9
Replies
41
Views
7 Months
Discussion Span
Last Post by can-mohan
0

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
0

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?

0

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.

1

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.

Edited by JamesCherrill

0

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

2

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.

Votes + Comments
QuantumC++ would have to be used to hold all values and answers but that compiler is not out yet.
2

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();
    }
};
0

Thanks all for your efforts and sharing the valuable informations.

Edited by can-mohan

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.