RE: getting subclass type from base class pointer

Paul Marquess <[email protected]>
Newsgroups gmane.comp.gdb.devel
Message-ID <CY1PR0501MB11786B043D21E72D27B73A8D95200@CY1PR0501MB1178.namprd05.prod.outlook.com>
> From: [email protected] [mailto:[email protected]] 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;
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.