GDB 7.11
As of GDB 7.11, GCC 5.3.1, Ubuntu 16.04, doing just:
p *myBase
on something compiled with:
gcc -O0 -ggdb3
may be enough as it already shows:
$1 = {_vptr.MyBase = 0x400c00 <vtable for MyDerived1+16>}
where MyDerived1
is the current derived class we are looking for.
But if you do in addition:
set print object on
the output is even clearer and looks like:
$1 = (MyDerived1) {<MyBase> = {_vptr.MyBase = 0x400c00 <vtable for MyDerived1+16>}, <No data fields>}
This also affects other commands like:
ptype myBase
which shows:
type = /* real type = MyDerived1 * */
class MyBase {
public:
virtual int myMethod(void);
} *
instead of:
type = class MyBase {
public:
virtual int myMethod(void);
} *
In this case, there was no indication of the derived type without set print object on
.
whatis
is similarly affected:
(gdb) whatis myBase
type = MyBase *
(gdb) set print object on
(gdb) whatis myBase
type = /* real type = MyDerived1 * */
MyBase *
Test program:
#include <iostream>
class MyBase {
public:
virtual int myMethod() = 0;
};
class MyDerived1 : public MyBase {
public:
virtual int myMethod() { return 1; }
};
class MyDerived2 : public MyBase {
public:
virtual int myMethod() { return 2; }
};
int main() {
MyBase *myBase;
MyDerived1 myDerived1;
MyDerived2 myDerived2;
myBase = &myDerived1;
std::cout << myBase->myMethod() << std::endl;
myBase = &myDerived2;
std::cout << myBase->myMethod() << std::endl;
}