开发者

Going through members of a C++ class

开发者 https://www.devze.com 2023-02-01 12:52 出处:网络
As far as I know, if I have a class such as the following: class TileSurface{ public: Tile * tile; enum Type{

As far as I know, if I have a class such as the following:

class TileSurface{
   public:
      Tile * tile;
      enum Type{
         Top,
         Left,
         Right
      };
      Type type;
      Point2D screenverts[4]; // it's a rectangle.. so..
      TileSurface(Tile * thetile, Type thetype);
}开发者_Python百科;

There's no easy way to programatically (using templates or whatever) go through each member and do things like print their types (for example, typeinfo's typeid(Tile).name()).

Being able to loop through them would be a useful and easy way to generate class size reports, etc. Is this impossible to do, or is there a way (even using external tools) for this?


Simply not possible in C++. You would need something like Reflection to implement this, which C++ doesn't have.

As far as your code is concerned after it is compiled, the "class" doesn't exist -- the names of the variables as well as their types have no meaning in assembly, and therefore they aren't encoded into the binary.

(Note: When I say "Not possible in C++" I mean "not possible to do built into the language" -- you could of course write a C++ parser in C++ which could implement this sort of thing...)


No. There are no easy way. If to put "easy way" aside then with C++ you can do anything imaginable.

If you want just to dump your data contents run-time then simplest way is to implement operator<<(ostream&,YourClass const&) for each YourClass you are interested in. Bit more complex is to implement visitor pattern, but with visitor pattern you may have different reports done by different visitors and also the visitors may do other things, not only generate reports.

If you want it as static analysis (program is not running, you want to generate reports) then you can use debugger database. Alternatively you may analyze AST generated by some compilers (g++ and CLang have options to generate it) and generate reports from it.

If you really need run-time reflection then you have to build it into your classes. That involves overhead. For example you may use common base-classes and put all data members of classes into array too. It is often done to communicate with applications written in languages that have reflection on more equal grounds (oldest example is Lisp).


I beg to differ from the conventional wisdom. C++ does have it; it's not part of the C++ standard, but every single C++ compiler I've seen emits metadata of this sort for use by the debugger.

Moreover, two formats for the debug database cover almost all modern compilers: pdb (the Microsoft format) and dwarf2 (just about everything else).


Our DMS Software Reengineering Toolkit is what you call an "external tool" for extractingt/transforming arbitrary code. DMS is generalized compiler technology parameterized by explicit langauge definitions. It has language definitions for C, C++, Java, COBOL, PHP, ...

For C, C++, Java and COBOL versions, it provides complete access to parse trees, and symbol table information. That symbol table information includes the kind of data you are likely to want from "reflection". If you goal is to enumerate some set of fields or methods and do something with them, DMS can be used to transform the code (or generate derived code) according to what you find in the symbol tables in arbitrary ways.


If you derive all types of the member variables from your common typeinfo-provider-baseclass, then you can get that. It is a bit more work than like in Java, but possible.


External tools: you mentioned that you need reports like class size, etc.--

Doxygen could help http://www.doxygen.nl/manual/features.html to generate class member lists (including inherited members).

0

精彩评论

暂无评论...
验证码 换一张
取 消