Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Multiple Inheritance vs. Interface

Reply
Thread Tools

Multiple Inheritance vs. Interface

 
 
lieve again
Guest
Posts: n/a
 
      09-20-2012
Hi!
I have a question regarding the implementation of the multiple
inheritance in C++.
As far as I know, the implementation problem of multiple inheritance
(one of them) in every programming language is the need for an extra
pointer for each new inherited class.
For example:
class Base1{
// hidden Base1_vtr = &Base1_vtbl[0]
virtual void func();
virtual void func2();
virtual void func3();
};
class Base2{
// hidden Base2_vtr = &Base2_vtbl[0]
virtual base2func();
virtual base2func2();
};

class NormalInheritance : public Base1 {
// hidden Base1_vtr = &Base1_vtbl[0]
virtual void anotherFunc();
};

class MultipleInheritance : public Base1, Base2 {
// hidden Base1_vtr = &Base1_vtbl[0]
// hidden Base2_vtr = &Base2_vtbl[0]
virtual void anotherFunc();
};

So, sizeof(Base1) == sizeof(Base2) == sizeof(NormalInheritance) == 4
bytes (only one virtual pointer)
but sizeof(MultipleInheritance) == 8 bytes
if class MultipleInheritance would inherit from another Base3, the
size would be 12 bytes and so on.

So with multiple inheritance we ends with big classes because of the
need of extra virtual pointers,
to avoid that, almost every language doesn't implement multiple
inheritance but Interfaces, where one
can only inherit more than one class but being that classes abstract
or pure virtual, like:

class Base1{
virtual void func() = 0;
virtual void func3();
};

class MixedClass : public NormalClass implements Base1, Base2,
Base...

My question is: Don't we have the same implementation problem as in C+
+? Because even being these classes
abstract, they need a virtual pointer. Why do they impose that rule
in languages like C#, Java, D...?
Someone know the reason?

 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      09-22-2012
On Thursday, September 20, 2012 3:59:04 PM UTC+3, lieve again wrote:
> So, sizeof(Base1) == sizeof(Base2) == sizeof(NormalInheritance) == 4
> bytes (only one virtual pointer)
> but sizeof(MultipleInheritance) == 8 bytes
> if class MultipleInheritance would inherit from another Base3, the
> size would be 12 bytes and so on.


It depends on implementation. "virtual" is just a keyword in C++ language.
While vtable is one usual way to implement it ... and multiple vtables is one
way to implement multiple inheritance ... it is NOWHERE written that you have
to do so. The programs written in C# are about 5 times less efficient than same
in C++ and so you could draw false conclusions that multiple inheritance is 5
times more efficient than no multiple inheritance.

> So with multiple inheritance we ends with big classes because of the
> need of extra virtual pointers,


You are mixing classes with objects here. You should stop. Just take few steps
back and learn basics. What is class what is object and what is the difference.
 
Reply With Quote
 
 
 
 
Aitor
Guest
Posts: n/a
 
      09-22-2012
> > So with multiple inheritance we ends with big classes because of the
> > need of extra virtual pointers,

>
> You are mixing classes with objects here. You should stop. Just take few steps
> back and learn basics. What is class what is object and what is the difference.


First thank you for the answers.

I didn't mean classes but object or the instances of the class:

Base1 base1; // sizeof(base1) == 4 bytes
MultipleInheritance multiple; // sizeof(multiple) == 8 bytes

I meant, supposing the vtable implementation is being used (by far the
most used), we are adding to any object or instance of the class an
extra pointer.

About the diamond problem, still complex, can be solved with the
virtual inheritance and the scope resolution operator:

multiple->Base1::func();
multiple->Base2::func();

So, my question was what is the benefit or making that classes
abstract or pure virtual in languages like C#, D...
We have the same extra pointers in the object, haven't we?

Cheers, lieve
 
Reply With Quote
 
lieve again
Guest
Posts: n/a
 
      09-22-2012
> > So with multiple inheritance we ends with big classes because of the
> > need of extra virtual pointers,

>
> You are mixing classes with objects here. You should stop. Just take few steps
> back and learn basics. What is class what is object and what is the difference.


First thank you for the answers.

I didn't mean classes but object or the instances of the class:

Base1 base1; // sizeof(base1) == 4 bytes
MultipleInheritance multiple; // sizeof(multiple) == 8 bytes

I meant, supposing the vtable implementation is being used (by far
the
most used), we are adding to any object or instance of the class an
extra pointer.

About the diamond problem, still complex, can be solved with the
virtual inheritance and the scope resolution operator:

multiple->Base1::func();
multiple->Base2::func();

So, my question was what is the benefit or making that classes
abstract or pure virtual in languages like C#, D...
We have the same extra pointers in the object, haven't we?

Cheers, lieve
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      09-22-2012
On Saturday, September 22, 2012 1:39:53 PM UTC+3, Aitor wrote:
> I didn't mean classes but object or the instances of the class:
>
> Base1 base1; // sizeof(base1) == 4 bytes
> MultipleInheritance multiple; // sizeof(multiple) == 8 bytes
>
> I meant, supposing the vtable implementation is being used (by far the
> most used), we are adding to any object or instance of the class an
> extra pointer.
>
> About the diamond problem, still complex, can be solved with the
> virtual inheritance and the scope resolution operator:
>
> multiple->Base1::func();
> multiple->Base2::func();
>
> So, my question was what is the benefit or making that classes
> abstract or pure virtual in languages like C#, D...
> We have the same extra pointers in the object, haven't we?


Benefit is that you can derive a class only from one class that actually
implements something. So one of ways to achieve serious complexity (make deep
and wide inheritance hierarchies) is removed from your toolset. As result you
can not burn the brains of novice maintainer who has to work on your code with
the class hierarchy.

Most modern programming languages try to be simple to use and to understand.
C++ trys to be efficient and feature-rich instead. C++ can be also simple
to use and to understand but then you have to agree with your team that
you follow certain idioms strictly and avoid using lot of things that are
legal C++ by standard.
 
Reply With Quote
 
Richard Damon
Guest
Posts: n/a
 
      09-22-2012
On 9/20/12 8:59 AM, lieve again wrote:
>
> So, sizeof(Base1) == sizeof(Base2) == sizeof(NormalInheritance) == 4
> bytes (only one virtual pointer)
> but sizeof(MultipleInheritance) == 8 bytes
> if class MultipleInheritance would inherit from another Base3, the
> size would be 12 bytes and so on.
>

And this should be expected as we would expect
sizeof(MultipleInheritance) = sizeof(Base1) + sizeof(Base2) +
sizeof(stuff added in MultipleInheritance)

as we would normally expect that each of the base classes be fully
represented within the derived class so it is easy to treat the derived
class as if it was any of its base classes. (There is an exception for
empty base classes which must have a sizeof > 0 as a class by itself,
but might not take any extra room when derived from.)

> So with multiple inheritance we ends with big classes because of the
> need of extra virtual pointers,
> to avoid that, almost every language doesn't implement multiple
> inheritance but Interfaces, where one
> can only inherit more than one class but being that classes abstract
> or pure virtual, like:
>
> class Base1{
> virtual void func() = 0;
> virtual void func3();
> };
>
> class MixedClass : public NormalClass implements Base1, Base2,
> Base...
>
> My question is: Don't we have the same implementation problem as in C+
> +? Because even being these classes
> abstract, they need a virtual pointer. Why do they impose that rule
> in languages like C#, Java, D...?
> Someone know the reason?
>


When inheriting from Interfaces, the difference is that the Interface
never needs to exist as a discrete object, so there isn't a need to save
a vtable pointer for each Interface. The Interface routine likely need a
pointer into the base class vtable to the vtable for that interface, but
that should be computable from the object normal vtable pointer.

The main reason many languages don't implement multiple inheritance (but
maybe Interfaces) is NOT object size, but language a program complexity.

 
Reply With Quote
 
Stuart
Guest
Posts: n/a
 
      09-23-2012
On 9/20/12 "lieve again" wrote:
> Hi!
> I have a question regarding the implementation of the multiple
> inheritance in C++.
> As far as I know, the implementation problem of multiple inheritance
> (one of them) in every programming language is the need for an extra
> pointer for each new inherited class.
> For example:
> class Base1{
> // hidden Base1_vtr = &Base1_vtbl[0]
> virtual void func();
> virtual void func2();
> virtual void func3();
> };
> class Base2{
> // hidden Base2_vtr = &Base2_vtbl[0]
> virtual base2func();
> virtual base2func2();
> };
>
> class NormalInheritance : public Base1 {
> // hidden Base1_vtr = &Base1_vtbl[0]
> virtual void anotherFunc();
> };
>
> class MultipleInheritance : public Base1, Base2 {
> // hidden Base1_vtr = &Base1_vtbl[0]
> // hidden Base2_vtr = &Base2_vtbl[0]
> virtual void anotherFunc();
> };
>
> So, sizeof(Base1) == sizeof(Base2) == sizeof(NormalInheritance) == 4
> bytes (only one virtual pointer)
> but sizeof(MultipleInheritance) == 8 bytes
> if class MultipleInheritance would inherit from another Base3, the
> size would be 12 bytes and so on.


That's right.


> So with multiple inheritance we ends with big classes because of the
> need of extra virtual pointers,
> to avoid that, almost every language doesn't implement multiple
> inheritance but Interfaces,



I don't think that this is the reason why the implementors of other
languages chose a single-inheritance approach. In my opinion it is the
complexity of a multiple-inheritance language that puts off a lot of
low-end programmers, but these programmers are the bulk of the
industry's employees.


> where one
> can only inherit more than one class but being that classes abstract
> or pure virtual, like:
>
> class Base1{
> virtual void func() = 0;
> virtual void func3();
> };
>
> class MixedClass : public NormalClass implements Base1, Base2,
> Base...
>
> My question is: Don't we have the same implementation problem as in C+
> +?



Yes, single inheritance languages that are implemented using vtables
still suffer from this problem.


> Because even being these classes
> abstract, they need a virtual pointer. Why do they impose that rule
> in languages like C#, Java, D...?
> Someone know the reason?



We can only guess. Unfortunately, there is no single person that has
perceived the Java programming language, so we cannot ask somebody (in
contrast to Mr. Stroustrup, who, although rarely, can be seen in this
newsgroup).

Regards,
Stuart
 
Reply With Quote
 
lieve again
Guest
Posts: n/a
 
      09-23-2012
On 23 sep, 16:32, Stuart <(E-Mail Removed)> wrote:
> On 9/20/12 "lieve again" wrote:
>
>
>
>
>
>
>
>
>
> > Hi!
> > I have a question regarding the implementation of the multiple
> > inheritance in C++.
> > As far as I know, the implementation problem of multiple inheritance
> > (one of them) in every programming language is the need for an extra
> > pointer for each new inherited class.
> > For example:
> > class Base1{
> > * // hidden Base1_vtr = &Base1_vtbl[0]
> > * virtual void func();
> > * virtual void func2();
> > * virtual void func3();
> > * };
> > * class Base2{
> > * *// hidden Base2_vtr = &Base2_vtbl[0]
> > * virtual base2func();
> > * virtual base2func2();
> > * };

>
> > * class NormalInheritance : public Base1 {
> > * // hidden Base1_vtr = &Base1_vtbl[0]
> > * virtual void anotherFunc();
> > * };

>
> > * class MultipleInheritance : public Base1, Base2 {
> > * // hidden Base1_vtr = &Base1_vtbl[0]
> > * // hidden Base2_vtr = &Base2_vtbl[0]
> > virtual void anotherFunc();
> > };

>
> > So, sizeof(Base1) == sizeof(Base2) == sizeof(NormalInheritance)== 4
> > bytes (only one virtual pointer)
> > but sizeof(MultipleInheritance) == 8 bytes
> > if class MultipleInheritance would inherit from another Base3, the
> > size would be 12 bytes and so on.

>
> That's right.
>
> > So with multiple inheritance we ends with big classes because of the
> > need of extra virtual pointers,
> > to avoid that, almost every language doesn't implement multiple
> > inheritance but Interfaces,

>
> I don't think that this is the reason why the implementors of other
> languages chose a single-inheritance approach. In my opinion it is the
> complexity of a multiple-inheritance language that puts off a lot of
> low-end programmers, but these programmers are the bulk of the
> industry's employees.
>
> > where one
> > can only inherit more than one class but being that classes abstract
> > or pure virtual, like:

>
> > class Base1{
> > * virtual void func() = 0;
> > * virtual void func3();
> > * };

>
> > * class MixedClass : public NormalClass implements Base1, Base2,
> > Base...

>
> > * My question is: Don't we have the same implementation problem as inC+
> > +?

>
> Yes, single inheritance languages that are implemented using vtables
> still suffer from this problem.
>
> *> Because even being these classes
>
> > * abstract, they need a virtual pointer. Why do they impose that rule
> > in languages like C#, Java, D...?
> > * Someone know the reason?

>
> We can only guess. Unfortunately, there is no single person that has
> perceived the Java programming language, so we cannot ask somebody (in
> contrast to Mr. Stroustrup, who, although rarely, can be seen in this
> newsgroup).
>
> Regards,
> Stuart


Ok, so the main reason for not implementing multiple inheritance
(without workarounds) is the complexity added to the programmers
(learning curve) and to the compiler developers (diamond
problem, ...). I thought maybe making the interfaces pure virtual,
there was a way to avoid the extra vpointers and I wanted to know how.
Then if I start adding pure virtual classes to impose the derived
classes with some kind of features like:
class Derived : implements Readable, Writeable, Comparable,
Convertible ...
regardless of the programming language, we are ending with instances
of the derived classes having 20 bytes or more even being those
classes with no members or empty. It is good to know.

Regards,
lieve
 
Reply With Quote
 
Öö Tiib
Guest
Posts: n/a
 
      09-23-2012
On Sunday, 23 September 2012 23:40:42 UTC+3, lieve again wrote:
> Ok, so the main reason for not implementing multiple inheritance
> (without workarounds) is the complexity added to the programmers
> (learning curve) and to the compiler developers (diamond
> problem, ...).


Yes, that is the reason. Also the solution, virtual inheritance is
not too efficient nor simple.

> I thought maybe making the interfaces pure virtual,
> there was a way to avoid the extra vpointers and I wanted to know how.
> Then if I start adding pure virtual classes to impose the derived
> classes with some kind of features like:
> class Derived : implements Readable, Writeable, Comparable,
> Convertible ...
> regardless of the programming language, we are ending with instances
> of the derived classes having 20 bytes or more even being those
> classes with no members or empty. It is good to know.


The bytes actually are cheap these days ... unless you write for some 8 bit
controller. On common platforms most of the memory goes into visuals
and sounds and helper texts and other massive data like that. Couple of bytes
for vtables of object that manages such data are usually not worth talking
about.

OTOH on the 8-bit controllers where you really count bytes you do not have
much need for such large class hierarchy anyway.

 
Reply With Quote
 
lieve again
Guest
Posts: n/a
 
      09-26-2012
On 23 Sep., 23:25, Öö Tiib <(E-Mail Removed)> wrote:
> On Sunday, 23 September 2012 23:40:42 UTC+3, lieve again *wrote:
> > Ok, so the main reason for not implementing multiple inheritance
> > (without workarounds) is the complexity added to the programmers
> > (learning curve) and to the compiler developers (diamond
> > problem, ...).

>
> Yes, that is the reason. Also the solution, virtual inheritance is
> not too efficient nor simple.
>
> > I thought maybe making the interfaces pure virtual,
> > there was a way to avoid the extra vpointers and I wanted to know how.
> > Then if I start adding pure virtual classes to impose the derived
> > classes with some kind of features like:
> > class Derived : implements Readable, Writeable, Comparable,
> > Convertible ...
> > regardless of the programming language, we are ending with instances
> > of the derived classes having 20 bytes or more even being those
> > classes with no members or empty. It is good to know.

>
> The bytes actually are cheap these days ... unless you write for some 8 bit
> controller. On common platforms most of the memory goes into visuals
> and sounds and helper texts and other massive data like that. Couple of bytes
> for vtables of object that manages such data are usually not worth talking
> about.
>
> OTOH on the 8-bit controllers where you really count bytes you do not have
> much need for such large class hierarchy anyway.


Ok, so its a problem suffered from all the actual programming
languages, I think it could be a kind of limitation to obtain so big
objects, but its so.
Maybe the way to impose some kind of properties or functions to a
class without the vpointers replication penalty is the concepts
extension of C++11.

Regards,
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
inheritance, multiple inheritance and the weaklist and instance dictionaries Rouslan Korneychuk Python 8 02-10-2011 04:02 AM
Interface inheritance vs Implementation inheritance. Daniel Pitts Java 27 02-27-2008 01:37 AM
Multiple inheritance/interface delegate through template function not working roman.blackhammer@gmail.com C++ 7 07-03-2007 09:00 AM
Multiple inheritance: Interface problem workaround, please comment this Axel Straschil Python 6 04-11-2005 08:14 AM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM



Advertisments