I recenty had a telephone interview in which I was asked about the internal implementation of the virtual mechanism.

I described roughly that a table is compiled with the class (at the top of the memory block?) which holds pointers to the addresses of the appropriate method implementation according to which derived class the object is, so when a base pointer is used to access a virtual method, I guess its compiled so that it infact references to an index in the vtable which for the given instance at run-time would be the address of the appropriate derived method.

However I could give precise details of the internal implementation of the table or say how to access it from client code, so I failed the question.

Is this part of the C++ standard or is it compiler specific, I'm having difficulty finding more detail on the internals of this in the Bjarne Stroustrup book, may be I'm looking in the wrong place.

Forum discussion on this is often varied and not always easy to follow, aso not official, is there an ansii doc. Do I need a C++ compiler text book, I looked on Amazon but there were very few books and no descriptions or reviews.

Even if I did know this in detail, isn't this the kind of thing one would restudy a reference for if one were going to implement something in relation to it. I think my agent now thinks I don't know C++ cos I flunked this one question. The vacancy wasn't for safety critical or driver implementation or anything like that!!

Any help or advice greatly appreciated.

>However I could [not] give precise details of the internal implementation
>of the table or say how to access it from client code, so I failed the question.

Then the question itself is invalid. The exact implementation of the vtable mechanism is compiler-specific and how to access it from client code is both non-portable and exceedingly stupid. If an employer asks this question and doesn't accept the answer you gave, you don't want to work there because they focus more in trivia than practical abilities.

Your high level view is about as good as it gets for a general question. In fact, the C++ standard doesn't require that an implementation use a vtable to implement polymorphism. Therefore, if the question wants specifics, it should mention a specific implementation, in which case it's no longer a test of your C++ knowledge, but a test of your knowledge of a single compiler.

However, there are a couple of books that describe how vtables typically work internally, given the full understanding that exact details will vary from compiler to compiler. In my experience, knowing the ways polymorphism can be implemented will help solidify your understanding of the big picture and give you more confidence, but unless you write C++ compilers you're not likely to use that knowledge in real world code.

However, there are a couple of books that describe how vtables typically work internally, given the full understanding that exact details will vary from compiler to compiler. In my experience, knowing the ways polymorphism can be implemented will help solidify your understanding of the big picture and give you more confidence, but unless you write C++ compilers you're not likely to use that knowledge in real world code.

The little pictures with boxes and arrows pointing about always left me a bit unsatisfied. I felt that as a coder I had to be able to doodle around with the addresses and pointers and so forth just to prove to myself I knew what was going on in memory, and that I was capable of the abstruce thought processes necessary to understand it all. Here is a disuussion I started on this topic quite some time ago, and was most appreciative that a very knowlegeable member helped me out...

http://www.daniweb.com/forums/thread144510.html

Some time after that discussion I wrote a few tutorials on my explorations of VTABLES and COM memory layout, and they can be found here...

http://www.jose.it-berater.org/smfforum/index.php?PHPSESSID=0b27761d455031d08871f877a1448ac9&board=362.0

While this latter is in a PowerBASIC oriented forum, most of the code in question is C++ code.

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.