Easy way to dump attributes of a class

closed account (48bpfSEw)
Hi, I'm looking for a method to dump all attributes of a class with one function.

Example:

class A { int a,b,c; }
class B { int d,e,f,g; }

I have to write for each class a function and I have to know the names of the attributes :

dump(A v) {
cout << v.a << endl << v.b << endl << v.c;
}

dump(B v) {
cout << v.d << endl << v.e ... etc;
}


I am looking for a methode to access all attributes abstractly of any given class :

template <A> dump (A v) {
for (int i=0; i<count_of_attributes;i++)
cout << v.getValueOfAttribute(i) << endl;
}


Does anyone has a good idea?
If you want write to a stream, you could overload the output stream (<<) operator for that class.

Example: https://ideone.com/bC7YgW
Last edited on
closed account (48bpfSEw)
Thank you iHutch.

Next time I'll define my class-structures with Excel, so I can generate automatically code for the dump with string-concatenations...
Having trouble discerning whether or not that's a genuine approach or barbed sarcasm.

Either way, what you're asking for - some mechanism that automagically prints the attributes of a number of arbitrary classes, regardless of their quantity or complexity - is probably not possible. If it is, I'd wager there's a good amount of hackiness involved.

A better question is why would all of these classes need dump out their attributes to stdout?

And the answer, I suspect, is they don't. You just need a debugger.
closed account (48bpfSEw)
Hi iHutch, I am working with the C++ Builder XE7 which has a debugger but it crashes continously. Therefore I dump all necessary information with OutputDebugString().

The idea with Excel is indeed a genuine approach! The base of any code is the data structure. Play a bit with this idea: define a class with Excel and you'll see that you can derive the CREATE TABLE-Statement, SELECT, UPDATE, DELETE-Statement, the Read() and Write() Operations for your sourcecode and the class-declarations self ... and for my purposes (I caught the idea after my posting) generate the code for debug-informations. it is very simple to work/develop from this point of view.

Last edited on
I am working with the C++ Builder XE7 which has a debugger but it crashes continously. Therefore I dump all necessary information with OutputDebugString().

Are you saying the debugger itself crashes, or your program?

I've used an earlier version of C++ Builder and found the debugger very useful, it would be a first and often only tool needed for identifying the cause of program crashes.

On the other hand there's nothing wrong with dumping relevant information in addition to using the debugger. Especially for finding logic errors, sometimes it is useful to generate a log file which gives a continuous record of the state of the variables, not just at the time of a crash.
closed account (48bpfSEw)
The debugger of C++ Builder 2007 was stable. Since XE7 it's horror trip to debug. We have currently a source code volume about 50 MB to compile & link...
Last edited on
In Delphi you have RTTI to access the meta data of an object, I think you have the same possibility in C++Builder if you r class inherits from TObject. Here is a link that show it in Delphi.

http://delphi.about.com/od/oopindelphi/a/delphirtti.htm
Topic archived. No new replies allowed.