public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* virtual table
@ 2013-11-20 16:30 Riccardo Manfrin
  2013-11-20 17:41 ` Ian Lance Taylor
  0 siblings, 1 reply; 2+ messages in thread
From: Riccardo Manfrin @ 2013-11-20 16:30 UTC (permalink / raw)
  To: gcc-help

Hi,

suppose I have three classes Foo, Bar, Zoo.

Foo inherits from Bar,
Bar inherits from Zoo.

Bar and Zoo are abstract classes (as defined here : 
http://en.wikibooks.org/wiki/C%2B%2B_Programming/Classes/Abstract_Classes)

The size of Foo shall account for the two pointers to Bar an Zoo vtables.

I was wondering if there is a way to have the Zoo vtable reference 
pointer embedded in the Bar vtable, so that Foo only has to store one 
pointer, while the second vtable reference is resolved when accessing 
the first. I'm aware that there are motivations like casting or multiple 
inheritance behind having all vtables pointers attached to the object 
instance in "ram" memory, rather then within the vtable "in rom", but I 
was wondering if there was a more "ram-efficient" way to handle this (as 
the vtables and the way the work is not dictated by the standard), which 
gcc could support.

R

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: virtual table
  2013-11-20 16:30 virtual table Riccardo Manfrin
@ 2013-11-20 17:41 ` Ian Lance Taylor
  0 siblings, 0 replies; 2+ messages in thread
From: Ian Lance Taylor @ 2013-11-20 17:41 UTC (permalink / raw)
  To: Riccardo Manfrin; +Cc: gcc-help

On Wed, Nov 20, 2013 at 6:53 AM, Riccardo Manfrin
<riccardomanfrin@gmail.com> wrote:
>
> suppose I have three classes Foo, Bar, Zoo.
>
> Foo inherits from Bar,
> Bar inherits from Zoo.
>
> Bar and Zoo are abstract classes (as defined here :
> http://en.wikibooks.org/wiki/C%2B%2B_Programming/Classes/Abstract_Classes)
>
> The size of Foo shall account for the two pointers to Bar an Zoo vtables.

For this kind of discussion it helps to show actual code.  If I
understand your description correctly, then your statement about the
size of Foo is incorrect.

#include <iostream>

class Zoo {
  virtual void zoo() = 0;
};

class Bar : public Zoo {
  virtual void bar() = 0;
};

class Foo : public Bar {
  void* v;
};

int
main()
{
  std::cout << sizeof(Foo) << std::endl;
}


On my x86_64 system this prints 16: 8 for the single vtable pointer, 8
for the void* field.  In other words, although Foo inherits from two
classes as you suggest, there is still only one vtable pointer.

Ian

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2013-11-20 16:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-11-20 16:30 virtual table Riccardo Manfrin
2013-11-20 17:41 ` Ian Lance Taylor

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).