Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Frankly, I don't understand the practical importance of vtable. For me it is just a theoretical concept which needs to be mugged up since interviewer will ask it surely. Can anyone shed some light on it that why interviewers love vtable? I don't see how knowledge of vtable makes me a competent c++ developer :-|

share|improve this question

migrated from stackoverflow.com May 31 '11 at 18:40

This question came from our site for professional and enthusiast programmers.

1  
What makes a good developer is the will to learn and understand things. If you can't get past a job interview without "cheating" are you really a good fit for the job ? –  ereOn May 31 '11 at 18:42
4  
Can't say I've ever been asked about vtables. polymorhism yes, but not vtables –  jk. May 31 '11 at 21:47
1  
Honestly I had no clue what you meant until I went to google and looked up vtable and saw that you meant Virtual Method Table..... There is that problem. I don't know what is to know aside from objects contain pointers to their virtual methods. I can't believe people would ask an interview question on this..... It would be better to see if they can do pointers...even implement a simple linked list as knowing a virtual method table is not going to affect code quality. Asking the difference between virtual and non virtual functions is fine..but who cares about the table...just the concept. –  Cervo Jun 5 '11 at 13:11

10 Answers 10

up vote 2 down vote accepted

It's just one way of checking if you know something. And I don't really enjoy that it's the deciding factor in a lot of places.

  1. Because most code doesn't even care about that sort of stuff
  2. Because vtables and run-time polymorphism and pure abstract classes are a whole lot of pain and spaghetti code in most places I've seen
  3. Because C/C++ is a vast field (template metaprogramming, run-time polymorphism, procedural coding, low level optimizations, algorithms and containers, POD, RAII, etc, etc.) and why template metaprogramming is worse than runtime-polymorphism for instance. STL/WTL vs COM/DCOM?
  4. Because it causes futureproofing things that don't have to be futureproof at expense of extra complexity
  5. Because of all the gotchas you add for no good reason most of the time
  6. Because of all the dynamic_cast stuff
  7. Because of all the virtual destructor stuff
  8. Because of all the runtime overhead
  9. etc.

Maybe it's just a coincidence, but every project that had runtime-polymorphism, vtabled classes, pure virtual classes were nightmare to maintain. Overengineered, spaghetti code, diamond problems, non virtual destructors, poor copy construction logics etc.

Ok, every good C++ programmer should be familiar with these concepts, and most gotchas that come with it, but why there is so much accent on this dangerous and overabused topic in interviews, I have no idea.

share|improve this answer
    
I've never seen such problems in over a decade of professional C++ programming. It sounds like your C++ programs were written by people incompetent in C++, who'd not know to avoid the diamond problem or when to make a destructor virtual. C++ is not an easy language to learn to use properly, but very powerful when it is properly used. –  David Thornley Jun 1 '11 at 15:26

It shows that you know how the virtual works, knowing that makes you a competent C++ developer.

share|improve this answer
2  
@ereOn: Unfortunately most of them are wrong to some degree. The knowledge most developers have (ie those that don't write compilers) is usually only the very basics and only half the truth when it comes to non trivial situations. –  Loki Astari May 31 '11 at 20:19
10  
As a competent C++ developer, I know very well what virtual functions do. I know this is implemented with a vtable. Exactly what's in that vtable and how it's used I don't know, and I don't see why I'd need to know. –  David Thornley May 31 '11 at 20:48
2  
@Mason: Then a whole lot of you are wrong. A C++ developer needs to know how virtual functions work according to the language. If the developer also knows that virtual calls are slightly slower, and objects with virtual calls will normally have an extra pointer, what more implementation details does the developer need? For that matter, how sure you are that your idea of a vtable is actually implemented the way you think in your favorite compiler? Purported knowledge that you haven't checked out isn't going to be useful. –  David Thornley May 31 '11 at 21:23
2  
@David, I don't know why you get so upset, but it seems that you do know how the vtables are implemented, at least have a general idea, as opposed to what you said before. So why do you think you should not have known it? Just to make a point - one of the bugs that you can have would be calling a pure virtual function in a destructor. Now, how can you explain why it is a bug, without knowing what a vtable is? How can you explain why not to do that without that knowledge? Is "don't do it" enough for you? Then again - you're not a competent developer. –  littleadv May 31 '11 at 21:26
3  
I rememeber years ago it was thought that to be a good C programmer you had to to be able to write assembly language. Is that still the case today? If not, why is the detail of vTables any different? –  mattnz Jun 1 '11 at 1:18

Like for any "good to know but not essential" concept, knowing what a vtable is shows that you know more than the average programmer.

share|improve this answer
5  
I would be more impressed if they knew what a "sequence point" was (though that may not be relevant in the next standard)). Know that and you already know more than 90% of developers. –  Loki Astari Jun 1 '11 at 4:53

Knowledge of what a vtable is does not make you a competent C++ developer; however, lack of that knowledge does imply that you are not a skilled C++ developer.

share|improve this answer
1  
It certainly doesn't make you a better developer, but it certainly implies you know more that the preson who can't answer the question properly. Since the OP's question is about interviewing, it's useful in creating the impression you're a better developer than the next person, even if that's not factually true. –  DaveE May 31 '11 at 19:56
3  
I completely disagree. Unless you are a compiler writer knowledge of the vtable is completely useless and often harmful to beginners. vtables are just an implementation details; knowledge of there existence does not help you understand the language. –  Loki Astari May 31 '11 at 20:17
1  
@Martin, have you ever implemented classes in C? I have, and I'm not a compiler writer. Knowing how vtables work is necessary when you actually need to implement it yourself. Thinking that only "compiler writers" do that shows lack of ability to think outside the box, IMHO. –  littleadv May 31 '11 at 20:23
3  
@littleadv: No I have never implemented classes in C,because I would switch languages to C++ when I need classes and more specifically inheritance.I have written a compiler or two though,and the trouble with implementing it yourself is that you are bound to get something wrong and then it is imposable to track down the problem (this is why the compiler builds the parts of the language need for run-time dynamic function calls and not the user). Also a one of half attempt at vtables for a very limited sub set of situations only gives you half the story (think multiple and/or virtual inheritance) –  Loki Astari May 31 '11 at 20:41
4  
@littleadv: Half the skill of being an engineer is knowing which language to use. Using C when you should have been using C++ was a mistake (a very interesting and fun mistake I am sure (I would have loved it)) but still a mistake. –  Loki Astari May 31 '11 at 20:48

Inheritance is an important object-oriented concept and c++ supports it. Almost all c++ frameworks use it in one way or the other. In c++, inheritance involves the usage of virtual methods. Testing the knowledge of how its all stitched up by the language, is a good way to measure your experience with c++.

share|improve this answer
5  
Except that it's not all stitched up by the language. It's a common (probably universal) implementation detail, and if you simply write C++ you will never encounter a vtable. –  David Thornley May 31 '11 at 20:49
    
Not universal. I have seen other implementations in the wild (though it was a long time ago, and I am way to old to digg into the guts of compilers now). –  Loki Astari Jun 1 '11 at 4:54
    
You can use inheritance in C++ without using virtual. In fact, C with classes did not have the virtual keyword, but inheritance was already possible. –  FredOverflow Jun 1 '11 at 18:18

Knowing about the vtable internals, you know how C++ differs from C. And other stuff such as overriding, inheritance etc.

share|improve this answer
1  
vtables can be, and are, done in C too. Before native C++ compilers, we had CFront that transliterated everything into C, if you need proof. –  JBRWilkinson May 31 '11 at 20:42
2  
@JBRWilkinson: CFront did not "transliterate" C++ into C. Rather, it compiled C++ and produced C as its object code. EDG ships their compiler with a back-end (intended primarily as a demo) that does the same (and Comeau, for one, distributes it to end users with the same or at least a similar back-end). –  Jerry Coffin May 31 '11 at 21:13
    
@Jerry: Curious about the difference between compile/transliterate –  Loki Astari Jun 1 '11 at 5:01
    
Actually, if anything it shows how close C++ is to C. Understanding vtables lets one understand how to call C++ code from C. –  Kevin Hsu Jun 1 '11 at 5:46
1  
@Martin: In a Russian-to-English translation, "zdrasvtvweetye" is a transliteration of a word, and "hello" is a translation. I'd call something like a C macro substitution a transliteration. –  David Thornley Jun 1 '11 at 15:31

Because tech interviewing is broken.

The interviewer doesn't know how to actually determine whether you can pad their bottom line -- in fact, I doubt they even have a clue just what their (company's) bottom line is!

Someone that is smart and gets things done can look up all they need to know about vtables with a quick search.

As I said up above, I used to do a lot of COM programming, and had to deal with vtables somewhat.

But I never had to deal with them in general/generic C++ programming.


EDIT:

Up above, we see a prime example of just how broken this process is:

Someone is claiming that if you don't know how vtables are implemented, then you don't know C++ and are, in fact, incompetent.

I've been lucky to have known 2 programmers that have been almost singlehandedly responsible for millions in revenue... and neither one of them knew about vtables.

One knew design patterns like the back of his hand, but the other was just barely above the "C with classes" level and apparently never heard of the DRY principle.

But they could both crank out code, and both made good products that were rock-solid and well-built.

share|improve this answer
3  
Spewing frustration doesn't help. If you have a solid method of selecting the best of the potential candidates - do share. –  littleadv Jun 1 '11 at 0:41
    
If you've been around for any length of time, then you'll know that Joel Spolsky (one of the founders of this site) has written extensively on this subject. You should start there. –  red-dirt Jun 1 '11 at 10:33
3  
As for frustration, like I said above, I could easily pass this question in an interview... At one point I could have coded a "C" solution on the white board. but you would be making a big mistake picking me over the two people I mentioned above. –  red-dirt Jun 1 '11 at 10:36
1  
I read Joel's blog, thank you. Sometimes, people make mistakes. Most of the time, when I interview people, I can detect whether they have an idea of C++ or not. Frankly, I myself never ask about vtables. But I do understand people who do. I have my own set of questions, which I'm sure someone will find irrelevant ect. So what? If you tell what are the questions that you ask - I'm sure I'll be able to criticize them just as you did in this thread. There's NO WAY to objectively detect talent. Even Joel cannot do that. –  littleadv Jun 1 '11 at 17:21
2  
@littleadv: To my knowledge, the C++ specification doesn't mention vtables. It is an implementation detail. Making any assumptions about it breaks the abstraction barrier the C++ language itself presents. What makes a developer good is the ability to deal with complexity. And to paraphrase Abelson and Sussman: In order to deal with complexity you must not rely on details. Asking for them is pretty much the interview-equivalent of bikeshedding. You have no idea how to determine, whether an interviewee can deal with complexity, so you become entrenched in pointless details. –  back2dos Jun 5 '11 at 11:47

I'm one of the interviewers at my company, and we ask a lot of details about C and C++ coding (yes, separately). I don't recall anyone asking about vtables in the interviews. Inheritance and polymorphism concepts, yes, but not vtables as such.

If you can properly and concisely explain what the keyword virtual means, let alone some of its drawbacks, you're above a lot of the people I've helped interview. We could ask about vtables, I suppose, but I think it would make me cry. Sort of like when my supervisor asks the candidate to rate their C++ knowledge, and we universally hear 8 out of 10 or 4 out of 5.

Ahem.

I'm not sure where you're interviewing, but there are good jobs and workplaces out there that don't ask about vtables.

share|improve this answer
    
It's funny how everybody thinks they are an 8 (why not a 7, I suppose there must be some sociological reason for that). But, I have heard that in the majority of interviews I have given. I have personally decided it has no meaning and stopped asking that question. –  Loki Astari Jun 1 '11 at 4:59
    
@Martin: I personally stopped asking it long ago, since it only ever makes me think less of the candidate. However, others in the (group) interview sometimes ask. –  Caleb Huitt - cjhuitt Jun 1 '11 at 14:05
1  
It's because absolutely noone will say they're a 10 for fear of getting their a$$ handed to them on the very next question. And since you can never be sure if a 9 is in the room (leading to a similar problem to 10, the best you can give is 8. Lower numbers mean you're arbitrarily less good that "good" developers, and nobody wants to end up in that situation in an interview. –  Kevin Jun 1 '11 at 14:49
1  
One suggestion I've picked up here or SO: ask "What can you do that a seven would have difficulty with, and what do you have difficulty with that a nine wouldn't?" –  David Thornley Jun 1 '11 at 15:28

+1 for being in the same boat. I was recently asked how virtual functions are implemented. I said that it is done usng V-Tables but didnt know the actual mechanism. The interviewer was pretty displeased and this was one of the questions that got me rejected.

I dont see how knowing V-Tables is a must have for a C++ programmer. Can I manipulate the VTable? No? Can I manually create one? No? Does knowing it help me tweak its performance? No? Can I atleast see it? No? So what benefit does it provide to know it? For instance , does knowing how the English language evolved make me a better speaker? No. Does knowing how the human brain makes sence of speech help me improve my language skills. No! I would rather spend my time knowing the things I can use.

But apparently this is lost on the "superstar" interiviewers who seem to have more time to learn how it done rather than to do it! It is just so fashionable to ask the VTable thinng that you wounldnt want to be caught dead not wearing it.

share|improve this answer

If you know what a vtable is, there's a good chance that you have a pretty good knowledge of C++ and can muck around with lower level programming if need be.

I'd have no qualms with a company considering hiring someone based on the fact that they know how vtables work. That said, I'd have a huge problem with a company that refuses to hire someone because they don't know how vtables work -- even for a C++ job.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.