virtual base class II

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
40

#include <iostream>
using namespace std;

class A
{
private:
  int a;
public:
  void changea(int);
  void displaya (void);
};

void A::changea(int b)
{
  a= b;
}

void A::displaya (void)
{
  cout << endl << a;
}

class B : public virtual A
{
public:
};

class C : public virtual A
{
public:
};

int main ( int argc, char** argv)
{
  C coffee;
  B tea;
  coffee.changea(10);
  tea.displaya();
}


When i enter value of a as 10 with coffee, and i try to print it out with tea, why do i not get the same value (10) and get some garbage out. If i have a virtual inheritance, should those values not be the same as only 1 object is created out of them ?
Last edited on
well, the member 'a' is not initialized. Therefore you get garbage when you don't initialize it with changea() explicitly which you don't do for 'tea'.
Ya! that is supposed to be the point. When i have just 1 object of A(since its a virtually called base), it shouldn't matter if i only initialize it with coffee and not tea ! shouldn't it ?
you misunderstand how base classes work, each object of type B and C have their own version of the member variable 'a' in the base class, there is not just one instance of the class 'A' that all derived classes share
Last edited on
its a virtual base class !!!
Regardless, MintBerryCrunch is correct.

so the virtual base class has only 1 object only when i inherit another class D from B and C ???

B/C are completely separate, and if you make a B and a C, they are still different.

As for your question, yes, since B/C have virtual inheritance D would have only one A inside it.
damn ! this concept is so un-intuitive ! but thanx a lot everyone !!!

~Cheers!
navderm
You think so? The derived class has it's members and the members of it's parents in the hierarchy. Just as multiple "B" objects don't use the same members as each other, neither do they use the same base class members.
@Zhuge :
Ya you're right. I wasn't thinking deep enough. Its a lot clearer now ! It just was confusing to see B and C get different A but D get only 1 out of those two As. But otherwise it creates a huge problem creating multiple objects of either B or C.

Thx a lot !!
You are misunderstanding the meaning of a virtual base class. And you are mixing up its meaning with the idea of a static class member.

The use of a virtual base class only comes into play when there is multiple inheritance, and more than one parent class inherit the same base class themselves. For instance, assume you created a new class D:
1
2
class D : public B, public C
{ };

If A were NOT a virtual base class of classes B and C, then D would have 2 copies of A when it is constructed. Calling changea() on an object of class D would cause confusion because it would only change one of the A::a values (either the one derived from B or the one derived from C).

By declaring A as a virtual base class of both B and C, class D will only have 1 copy of A within it. Calling changea on an object of class D in this case will cause no confusion, and the single copy of A::a will be changed appropriately. However, objects of class D would each have their own copy of A::a, so calling changea() on one object of class D would not affect the value in another object of class D.

To get the behavior you were expecting, you would want to make A::a a static member of class A. Then, every object of class A (or a class derived from A) would see the same value.

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
#include <iostream>
using namespace std;

class A
{
private:
        static int a;
public:
        void changea(int b);
        void displaya();
};

void A::changea(int b)
{
        a = b;
}

void A::displaya()
{
        cout << a << endl;
}

int A::a = 7;

class B : public A
{ };

class C : public A
{ };

int main()
{
        C coffee;
        B tea;
        coffee.changea(10);
        tea.displaya();
}


Is that what you're looking for?
Hey,
No. I did not want multiple copies of tea and coffee to have same value.
I just wanted 1 copy of tea and 1 copy of coffee to have same value...
and now i realize how stupid i was :(

But i still don't understand ... what is the mechanism used to find which A will D use... B::A or C::A
??

if two values (int a i.e.) of B::A and C::A are really different even for virtual inheritance, then the only use of such inheritance can be using member functions from the base class.

So can anyone give me a good example where i cannot do without virtual inheritance of such kind.
If you use virtual inheritance, there is only one A in D.

If you don't, there is two...I think you might be able to access them using this->B::A.whatever = 0;
or similar, but I don't see why you would want it.

As for your question, I haven't ever needed multiple inheritance (yet) so...
Topic archived. No new replies allowed.