This is the mail archive of the
gdb@sourceware.org
mailing list for the GDB project.
RE: getting subclass type from base class pointer
- From: Paul Marquess <Paul dot Marquess at owmobility dot com>
- To: "gdb at sourceware dot org" <gdb at sourceware dot org>
- Date: Thu, 29 Oct 2015 23:38:27 +0000
- Subject: RE: getting subclass type from base class pointer
- Authentication-results: sourceware.org; auth=none
- Authentication-results: spf=none (sender IP is ) smtp dot mailfrom=Paul dot Marquess at owmobility dot com;
- References: <CY1PR0501MB1178C26EDF7B171D6BDAE33E95200 at CY1PR0501MB1178 dot namprd05 dot prod dot outlook dot com>
- Spamdiagnosticmetadata: NSPM
- Spamdiagnosticoutput: 1:23
> From: gdb-owner@sourceware.org [mailto:gdb-owner@sourceware.org] On Behalf Of Paul Marquess
>
> I must be missing something obvious, so apologies upfront.
>
> Consider this simple snippet of C++
>
>
> class SuperClass
> {
> };
>
> class SubClass : public SuperClass
> {
> };
>
> int main()
> {
> SuperClass* pTest = new SubClass;
>
> delete pTest; // << Break here in GDB }
>
> Using the python API I'm trying to determine the type of object that pTest points to.
>
>
> gdb) python x = gdb.parse_and_eval("pTest")
> (gdb) python print x.type
> SuperClass *
> (gdb) python print x.dereference().type
> SuperClass
>
> How do I get at the SubClass object using the Python API?
Quick follow-up to my own post, the actual code I'm working with is more like shown below, so I there should be a vtable in play.
Same question - how get at the SubClass object if I have a pointer to a SuperClass object using the Python API?
class SuperClass
{
public:
virtual int fred() { return 1; }
SuperClass() {}
virtual ~SuperClass() {}
};
class SubClass : public SuperClass
{
public:
virtual int fred() { return 2 ;}
SubClass() {}
virtual ~SubClass() {}
};
int main()
{
SuperClass* pTest = new SubClass;
delete pTest;
}