Possible Duplicate:
How can I add reflection to a C++ application?
Like in python, we have dir() to list all the attributes/functions/members of an object, is there a way for C++ objects to get all the members, either in gdb or some other utility ?
Possible Duplicate:
How can I add reflection to a C++ application?
Like in python, we have dir() to list all the attributes/functions/members of an object, is there a way for C++ objects to get all the members, either in gdb or some other utility ?
C++ does not provide a built-in mechanism for achieving this. This topic seems pretty well covered in the discussion Keith Randall pointed out.
gdb
can provide to you the data members for your object, but you need to have an instance of the object to refer to, and do print a
, if the name of the global was a
. To find the methods, you would use info functions A::*
, if the class name was A
. This will not find inline functions (note baz_inline
in the example).
class A {
public:
void foo ();
int bar (int);
void baz_inline () {}
double d;
short p;
};
A a;
void A::foo () {}
int A::bar (int) { return 0; }
int main () {}
$ g++ -g c.cc
$ gdb a.out
(gdb) p a
$1 = {d = 0, p = 0}
(gdb) p &a.d
$2 = (double *) 0x600920
(gdb) p &a.p
$3 = (short *) 0x600928
(gdb) info functions A::*
All functions matching regular expression "A::*":
File c.cc:
int A::bar(int);
void A::foo();
(gdb) quit
Originally, I was thinking middleware, because there are middleware technologies that partially do what you want. There, the C++ class is defined in a meta-language (IDL - interface definition language), and the middleware tools generate C++ class definitions by parsing the IDL description of the class. One of the purposes for this was to allow code to be generated that understood how to package an object of that type, deliver it to another machine, unpack it at that machine, and let software on that machine use the object.
You have not described such a need, but you want similar features, in that you wanted to know about the C++ interface and its data members. Since you said python already had this facility, I was thinking it might be easy to write your own little tool that used python code as your IDL. That is, you create a python object that you actually want to be your C++ object, and your tool takes the result of dir()
to generate the C++ header file to represent that object. You could query the python version of the object (with embedded python) for the information you wanted to know about the C++ object.