Thread: Vtables riddle.

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    242

    Vtables riddle.

    I've come across this riddle:
    What will this code do -
    Code:
    (***someClass)();
    What will these 3 asterisks do?
    This relates to the virtual table and the __vptr.

    (someClass is AN INSTANCE OF an inherited/the base class; only this detail is known about the classes' inheritance)

    Any suggestions?
    Last edited by eXeCuTeR; 07-22-2008 at 01:06 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Hopefully someClass is something that, when dereferenced three times, is something that can take () (a function or similar).

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Probably a function which is overloaded in classes that inherit from someClass, what with all of the references to the vtable. http://en.wikipedia.org/wiki/Virtual_table
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by tabstop View Post
    Hopefully someClass is something that, when dereferenced three times, is something that can take () (a function or similar).
    Well it seems like it, but you can't be sure because you don't know if someClass is an instance of another class (which is the derived/base class) and it might that the () operator was overloaded or w/e. Personally, I don't think this is the point of this riddle, plus, it doesn't relate to virtual tables/the __vptr (which points to the virtual table)
    Last edited by eXeCuTeR; 07-22-2008 at 01:09 PM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by eXeCuTeR View Post
    Well it seems like it, but you can't be sure because you don't know if someClass is an object of another class (which is the derived/base class) and it might that the () operator was overloaded or w/e. Personally, I don't think this is the point of this riddle, plus, it doesn't relate to virtual tables/the __vptr (which points to the virtual table)
    But the () overloading/function call won't happen until the three *** are taken care of. It doesn't matter what the () would mean in someClass().

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    (someClass is inherited/is the base class; only this detail is known about the classes' inheritance)
    But with your example, someClass would have to be an instance of a class, wouldn't it?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by dwks View Post
    But with your example, someClass would have to be an instance of a class, wouldn't it?
    Oh, I'm sorry, you're right, I'll edit.
    And again, I assure you these 3 asterisks relate to the __vptr.
    Last edited by eXeCuTeR; 07-22-2008 at 01:33 PM.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Well, here's the only thing I can think of right now.

    Perhaps the * operator is overloaded in a base class, and overridden in inherited classes. The code that you have would then have to look at the virtual table to figure out which * operator to call. *shrugs*

    I think you're probably mistaken, and that it's the () that relates to the virtual table.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Even ignoring all type issues that the compiler will complain about for the dereferencing, and ignoring that even the existence of a vtable is an implementation detail (also known as "none of your business"), unless someClass is a pointer, you've got an asterisk too many.
    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

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Not necessarily. What if the * operator is overloaded? For example, Dinkumware doesn't seem to mind this:
    Code:
    #include <iostream>
    
    class c {
    public:
        c &operator *() { return *this; }
        void operator()() { std::cout << "Greetings.\n"; }
    };
    
    int main() {
        c someClass;
    
        (***someClass)();
    
        return 0;
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That has nothing to do with vtables, though

    I think the OP's goal is about the fact that most compilers implement virtual dispatch using a vptr as the first member of the class, so if you "dereference" the object, you get the vtable, which is an array of function pointers.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Complicated riddle?
    By eXeCuTeR in forum C Programming
    Replies: 20
    Last Post: 11-21-2007, 06:55 AM
  2. A riddle : )
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-04-2002, 11:36 AM
  3. riddle me this, batman
    By Aran in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 05-25-2002, 04:19 PM
  4. Pointer Riddle that is bugging me
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 09-29-2001, 11:11 PM
  5. Another riddle tread!
    By minime6696 in forum A Brief History of Cprogramming.com
    Replies: 6
    Last Post: 08-24-2001, 06:37 AM