How to print a c++ object members using GDB from an address if the object's class type is like A::B

I know that this is labeled as answered, but I was able to reproduce this problem using gdb on OS X (GNU gdb 6.3.50-20050815 (Apple version gdb-1820) (Sat Jun 16 02:40:11 UTC 2012)) and the works-for-me solution didn't answer it for me.

Turns out there was another question on SO that did have an answer which worked, so I think it's worth pulling into this quesiton:

Why gdb casting is not working?

The short answer is that you may have to single-quote your namespaced variables:

(gdb) p ('MyScope::MyClass'*) ptr;


Works for me:

g++ -g test.cpp -o test
gdb test
(gdb) break main
(gdb) r


Breakpoint 1, main () at test.cpp:22
22      A::B *p = new A::B(100);
(gdb) n
24      p->print();
(gdb) n
m_a is 100
26      int *q = 0;
(gdb) p p
$1 = (A::B *) 0x602010
(gdb) p (A::B *) 0x602010
$2 = (A::B *) 0x602010
(gdb) p *((A::B *) 0x602010)
$3 = {m_a = 100}

It works for me. What are you using (gcc version, OS, compilation flags?)

Tags:

Linux

C++

Gdb