当前位置: 动力学知识库 > 问答 > 编程问答 >

debugging - Viewing dynamic type in Netbeans debugger for C++

问题描述:

I have a class hierarchy which starts with

class Command { ... }

and then has subclasses

class RunCommand : public Command { ... }

In the Netbeans debugger, when viewing a variable of type Command * I would like to see the actual type of the variable,

Command * command [RunCommand]

and then be able to expand the field to view the state. Is this possible?

网友答案:

I did some research and it seems like NetBeans does not have such built in functionality.

Not to worry! You can achieve the same effect by setting up a custom watch point for the variable you want to find out the type of.

The code that I will be debugging:

#include <typeinfo>
#include <iostream>
using namespace std;
class Command {
public:
   virtual void bla() {
        cout << "Base";
    }
};
class RunCommand : public Command {
public:
    void bla() {
        cout << "Child";
    }
};
int main(int argc, char** argv) {    
    Command * base_command = new Command();
    Command * run_command = new RunCommand();   
    cout << "Breakpoint"; 
    delete base_command;
    delete run_command;
    return 0;
}

Note that I have included <typeinfo>. I will be stopping to look at the variables at the line printing "Breakpoint". Here is how the watch point looks like:

The watch expression are :

typeid(*base_command).__name
typeid(*run_command).__name

How it works:

I am using the typeid operator. I am passing it the object that I want to view the type of. Note that in our case we have to dereference the pointer to get to the object. The operator returns some kind of typeid object with member __name. That field seems to contain the runtime type of the object that is passed to typeid.

I am not aware how portable this is. I have done the above using NetBeans 8.0.2 and gcc 4.8.2.

分享给朋友:
您可能感兴趣的文章:
随机阅读: