none
vtable display in Visual Studio 2005 IDE not correct

    Question

  • Consider the below code snippet

    #include<conio.h>

    #include <iostream>

    using namespace std;

    class base

    {

    public:

    virtual void function()

    {

    cout<<"base class function()"<<endl;

    }

    };

    class derived : public base

    {

    public:

    void function()

    {

    cout<<"derived class function()"<<endl;

    }

    virtual void func2()

    {

    cout<<"derived class func2()"<<endl;

    }

    };

    class derived2 : public derived

    {

    public:

    void func2()

    {

    cout<<"derived 2 class func2()"<<endl;

    }

    };

    int main()

    {

    base b;

    derived d;

    derived2 d2;

    derived *dptr=new derived2();

    dptr->func2();

    getch();

    return 0;

    }

    The vtable shown in VS2005 is as shown below:

    - b {...} base
        - __vfptr 0x004410c8 const base::`vftable' *
             [0x0] 0x00415e5b base::function(void) *
     
    - d {...} derived
        - base {...} base
            - __vfptr 0x004410e8 const derived::`vftable' *
                 [0x0] 0x00415082 derived::function(void) *
    - d2 {...} derived2
        - derived {...} derived
            - base {...} base
                - __vfptr 0x00441130 const derived2::`vftable' *
                     [0x0] 0x00415082 derived::function(void) *

     

    NOTE: func2() is NOT available here. Why??
    It seems like 'derived' does not have a __vfptr even though it has a virtual function func2()
    Is this a bug in debugger/Watch window of Visual Studio .NET 2005 ?
    Or my understanding is wrong?
     
    But at the end of the code, when 'derived2' object is assigned to 'derived' pointer, and this pointer used to access func2(),
    the compiler correctly calls 'derived2's func2() which over-rides 'derived's func2().
     
    Please explain!!!

     

     
    Tuesday, July 15, 2008 1:25 PM

Answers


  • This seems to be a Debugger’s limitation. In order to see several (e.g. ten) entries from the table, try the following watch expression: ((void**)d.__vfptr),10 and then expand the tree. Only first values are valid. Hope this helps.

    Tuesday, July 15, 2008 1:43 PM

All replies


  • This seems to be a Debugger’s limitation. In order to see several (e.g. ten) entries from the table, try the following watch expression: ((void**)d.__vfptr),10 and then expand the tree. Only first values are valid. Hope this helps.

    Tuesday, July 15, 2008 1:43 PM
  • Yup, the debugger doesn't have sufficient type information to tell how long the array is.  So it only displays the first element unless overridden.
    Hans Passant.
    Tuesday, July 15, 2008 4:28 PM
  • Thanx... I am able to see the functions with the watch expression. Are these documented anywhere in MSDN?
    Wednesday, July 16, 2008 5:02 AM