Thread: Virtual Table

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    13

    Lightbulb Virtual Table

    Hello Forum

    I have a Very basic Doubt.

    Say here is a Base Class A, Base Class B
    both have one virtual function each a() and b() respectively.

    and

    Classes C, D, E, F.


    C inherits A

    D inherits B

    E inherits both A and B

    F inherits all A, B, C, D and E.




    The question is how many virtual tables of A would be created and how the entries of those tables would be.

    Thanks in advance.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Doubtless, this is homework. Good luck with that.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Isn't this rather implementation dependent?
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by anon View Post
    Isn't this rather implementation dependent?
    Without a doubt, we need to know which compiler it is [and if it's not a commonly used compiler, the answer would still be in doubt].

    F inherits all A, B, C, D and E.
    I'd not be surprised if this would be quite messy to tangle out which versions of a() and b() are called if C, D and E also implement a/b functions.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by anon View Post
    Isn't this rather implementation dependent?
    Yes, but unfortunately it's a popular interview question, at least in my experience.

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Yeah, but I doubt this is an interview question, I mean, how many interviews allow Internet access during it? Seems doubtful.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by medievalelks View Post
    Yes, but unfortunately it's a popular interview question, at least in my experience.
    And when you explain that it's implementation dependant and not defined in the standard, what is the reaction? It's like a question I got in an interview saying "What is the size of an int?" and I said "Depends"... I was then asked "What size is it USUALLY?", which made me respond 32.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Anyway, there's no compiler that creates more than one vtable for any given class.

    An entirely different question is how many vptrs F will contain.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by CornedBee View Post
    Anyway, there's no compiler that creates more than one vtable for any given class.
    But there is absolutely no rule in the C++ standards to say that there must be only one, right? So whilst any sane compiler (any existing compiler) would only create one, there is no rule stating that this MUST be the case. [I'm not arguing the case here, just making sure I understand for myself].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The C++ standard doesn't prescribe any implementation detail. For all it cares, compilers could do their dynamic dispatching using an if-else chain with string comparison of the class name.

    Hmm, but I may have to correct myself. It could be that E and F have more than one vtable. I don't think so, but it would reduce the complexity of generating the vtable to just use multiple ones.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    May 2008
    Location
    Paris
    Posts
    248
    F inherits all A, B, C, D and E.
    If C, D and E are derived from A and B, there is no use in deriving F from A and B as well...

  12. #12
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The example is not there to be useful

    But there is a difference between deriving them and not doing so.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    It doesn't even say how it inherits from the bases. I'd think it would be different where virtual inherritance was used.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by iMalc View Post
    It doesn't even say how it inherits from the bases. I'd think it would be different where virtual inherritance was used.
    Good point. Another flaw in the question. It is also strange that we haven't heard from the OP.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Maybe I'm just misunderstanding the problem here, but I always thought that every class that has at least one virtual function will also have a hidden v-ptr, which points to the class's v-table. i.e. there is only one v-table, which tells the class which flavour of the virtual function to call in it's inheritance tree. Since every function can at most have one such flavour, a single table isn't all that complex. You'd have at most one entry for every virtual function that can be accessed through the this pointer of that instance.
    Or am I wrong?

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 48
    Last Post: 09-26-2008, 03:45 AM
  2. Pure Virtual Function - Virtual Method Table
    By u_peerless in forum C++ Programming
    Replies: 8
    Last Post: 06-07-2008, 01:19 AM
  3. Writing array, to file
    By zootreeves in forum C Programming
    Replies: 9
    Last Post: 09-08-2007, 05:06 PM
  4. C++ XML Class
    By edwardtisdale in forum C++ Programming
    Replies: 0
    Last Post: 12-10-2001, 11:14 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM