3

There is one little related question. But the topic is entirely different.

Now, one concept is about the function resolution and another is about class resolution ? I am wondering that how is it possible if they are using the same vtable (at least in gcc-4.5) ? Is this a compiler dependent terminology ?

I know that it might appear as basic silly question, but I had never thought of it.

Community
  • 1
  • 1
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • 5
    It is clearly compiler dependent, since there is nothing in the C++ specification that mandates a vtable. – andrewdski Jun 26 '11 at 02:36
  • An implementation can certainly use a single vtable for both purposes. Why do you think it's impossible? What concrete operation would be difficult to perform with just one vtable? – n. m. could be an AI Jun 26 '11 at 02:51
  • I am not saying it's impossible, just wanted to know in brief that how it happens under the hood. – iammilind Jun 26 '11 at 02:56
  • 1
    @n.m: I can certainly understand why the OP is thinking about impossibility. As I recall, Bjarne related (possibly in design & evolution) how he had to painstakingly work out the details in order to convince himself that it was possible. It's not trivial, to wit, some compilers implement virtual inheritance incorrectly for some corner cases. – Cheers and hth. - Alf Jun 26 '11 at 03:18
  • I think you might be interested in [this paper](http://www.cse.wustl.edu/~mdeters/seminar/fall2005/mi.html), which is all about the virtual table (table), including virtual inheritence and its implications. :) – Xeo Jun 26 '11 at 03:22
  • @Xeo "_you might be interested in this paper_" you mean [this blog](http://tinydrblog.appspot.com/?p=89001)? – curiousguy Aug 17 '12 at 20:26

2 Answers2

3

A good reference for this sort of thing is the Itanium ABI - see eg http://mentorembedded.github.com/cxx-abi/abi.html#vtable. Despite the name it's a widely used ABI for C++ and it describes a good, working implementation (although obviously other implementations are possible).

curiousguy
  • 8,038
  • 2
  • 40
  • 58
Alan Stokes
  • 18,815
  • 3
  • 45
  • 64
1

You can solve both problems (virtual function calls and virtual inheritance) if you know the dynamic type of an object given just a pointer to it. Every (polymorphic) object in C++ has precisely one dynamic type, which is determined at the moment when it's constructed. E.g. when you write new Foo, that object has the dynamic type Foo even if you store just a void*.

A vtable is a mechanism to store information about the dynamic type of an object in such a way that it can be retrieved via a base pointer. You can store quite some things in a vtable: function pointers, cast offsets, std::type_info objects even.

MSalters
  • 173,980
  • 10
  • 155
  • 350