This is what I read about it.

virtual base class becomes common direct base for the derived class

So my question is exactly what does this mean.

What I believe it means.

class A
{
protected:
int a;
public:
virtual int get_a();
A(void);
~A(void);
};

With an inherited class of B

class B : public virtual B 
{
protected:
int b;
public:
virtual int get_b();
B(void);
~B(void);
};

My assumption is that when a program is created in C++ there is a batch of memory where the functions for each class are held. Whenever I do something like A.get_a() it grabs the function from that pool of memory and processes it like you would expect a function to do so. If I do a non-virtual inheritance my assumption is that it grabs get_a() function from the pool of memory set for class A. However, if it is virtually inherited than a copy of get_a() exists in both the pool memory for class A function and class B functions if directly inherited without modification as shown.

I think this is wrong because I don't see the utility of it unless virtual can be applied in some way I'm not aware. Like maybe a virtual static member so each virtual inherited member gets it's own copy because they might need to be slightly different.

Recommended Answers

All 2 Replies

Hi,

Inheritance with virtual functions and virtual inheritance are two different things. After reading your initial question, I think you first need to read about normal inheritance with virtual functions.

Virtual functions are for implementing polymorphism.
If a class is derived from another class and when you create a object of the derived class, contiguous memory is allocated for the entire object: memory needed for base class + derived class.

See the following example:

class A{
	int a;
        virtual int getInt(){ return a;}
};

class B : A{
	int b;
        virtual int getInt(){ return b;}
};

B objectB; // size of objectB will be 16, 4 for int a from class A and 4 from int b from class B and 8 byte for virtual function.

Notice only one pointer (8 byte) allocated for virtual function getInt() in vtable. Not two pointers for getInt() in A and getInt() in B.

I hope you know how virtual functions work, so i am not giving any example of how to use getInt(), but if you dont know, let me know.
for further reading, see below:
http://www.cplusplus.com/doc/tutorial/polymorphism/

Virtual inheritance is used to answer the problem of memory of multiple inheritance.
Notice following code:

class A{
	int a[1000];
};

class B : public  A{
	int b;
};

class C: public  A{
	int c;
};

class Final: public B, C{

};

//What will be the size of Final? there is common A through B and C, so how it will be treated? size of Final will be 8008, it allocated two int a[1000] for B and C

What will be the size of Final? there is common A through B and C, so how it will be treated? size of Final will be 8008, it allocated two int a[1000] for B and C
to avoid this you need virtual inheritance, then it will share memory for A.

see the code below:

class A{
		int a[1000];
	};
	
	class B : public virtual A{ //notice virtual
		int b;
	};
	
	class C: public  virtual A{ //notice virtual
		int c;
	};
	
	class Final: public B, C{ 
	
	};

Now size of final will be: 4032. as Final will have common A through B and C. Here is the break down of the memory allocation:
4000 for int array.
4 for int c in C + 4 byte padding.
4 for int b in B + 4 byte padding.
16 byte for vtable pointer.

I hope it helps to understand virtual inheritance.
For further reading:
http://en.wikipedia.org/wiki/Virtual_inheritance
http://www.parashift.com/c++-faq-lite/multiple-inheritance.html

commented: pretty good! +14

Now size of final will be: 4032. as Final will have common A through B and C. Here is the break down of the memory allocation:
4000 for int array.
4 for int c in C + 4 byte padding.
4 for int b in B + 4 byte padding.
16 byte for vtable pointer.

Just a correction on the breakdown of memory allocation. None of the classes have virtual functions, so they will not have vtable pointers. Here is the correct breakdown (in order):
4000 bytes for int array in A.
8 bytes for the pointer to the virtual base-class (A) object of B.
4 bytes + 4 bytes padding for the int b in B.
8 bytes for the pointer to the virtual base-class (A) object of C.
4 bytes + 4 bytes padding for the int c in C.
___
4032 bytes in total.

In order to implement virtual inheritance, all the classes that are virtually derived from a base-class will need a pointer to the "this" pointer of the virtual base-class because it could move with respect to the derived class's "this" pointer depending on the overall inheritance hierarchy.

I also recommend Marshall Cline's explanations of it. Here

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.