Results 1 to 3 of 3

Thread: C++ view data on derived type from base pointer

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    1

    C++ view data on derived type from base pointer

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

    virtual dump(std:stream &os) const = 0;
    }

    class B : public A {

    B():
    value(1)
    {}

    virtual dump(std:stream &os) const {
    os << value;
    }

    private:
    int value;
    }

    main() {
    ...
    A * a = new B;

    a.dump(cout);
    ...
    }


    When I dive on a, the data for a is solely the int(...)** virtual table pointer, but i knows that the type is B. Yet the window does not show the type as B, rather it displays it at A, therefore I do not see 'value'. Is there a way to get totalview to display the derived type information knowing the type via the virtual table pointer? Or do I have to change the type myself?

  2. #2
    Well, this one has been sitting around for a while without an answer but it has gotten a lot of views and I was asked about this today with reference to this post, so I'll put in my two cents. The debugger is only going to know as much as the compiler tells it. Given this bit of code, the variable 'a' is defined as Class A, even though we use the constructor for Class B. So, when you dive on it, the fact you see only Class A is due to the fact that the debug information for this variable declares it as Class A. Since we are using the constructor for B, you can change the type from Class A * to Class B * and then dive on the pointer to get to the entry for 'value'.

    Cheers,
    Pete Thompson
    TotalView Customer Services

  3. #3
    Actually, thanks to the virtual table and with the same information from the compiler, GDB can actually recover the complete type of 'a' (Using "set print object on"):
    (gdb) set print object on
    (gdb) p a
    $2 = (B *) 0x6020d0


    Here the type of 'a' is obvious, but this is often not the case. Is there a way to lobby for this feature?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •