How to use the vtable to determine class type

Posted by Alex Silverman on Stack Overflow See other posts from Stack Overflow or by Alex Silverman
Published on 2010-06-09T14:28:04Z Indexed on 2010/06/09 14:42 UTC
Read the original article Hit count: 211

Filed under:
|

I was recently on an interview for a position where C/C++ is the primary language and during one question I was told that it's possible to use the vtable to determine which class in a hierarchy a base pointer actually stores.

So if, for example you have

    class A  
    {  
    public:  
    A() {}  
    virtual ~A() {}  
    virtual void method1() {}  
    };

    class B : public A  
    {  
    public:  
    B() {}  
    virtual ~B() {}  
    virtual void method1() {}  
    };

and you instantiate A * pFoo = new B(), is it indeed possible to use the vtable to determine whether pFoo contains a pointer to an instance of A or B?

© Stack Overflow or respective owner

Related posts about c++

Related posts about vtable