print struct fields and its member structs' fields recursively, generically

C

call_me_anything

I have different kind of data structures in different applications.
The data structures are big and complex and I would like to print the
members of each struct.

Can we write a generic piece of code which will
automatically find out the type of struct elements and
print them (indented manner ?) if they are primitive types like
int, float or char* ? else it will recurse into the composite data
type.

This is to avoid writing such functions for each struct in each
application.
Sounds something close to gdb ?
 
C

call_me_anything

Can we extend the above printing to classes in C++
Java probably has a toString() method defined automatically for every
object that
does such a kind of job.
 
G

Guest

Can we extend the above printing to classes in C++
Java probably has a toString() method defined automatically for every
object that
does such a kind of job.

That would require reflection, something that C++ does not have. If you
want to print the contents of a struct you will have to write a method
that does the printing. Preferably you overload the << operator, if you
do this for every struct it will be quite easy. To get nice indentation
on the other hand will be quite hard unless you hard code the indention
level of each struct. All of the above is also applicable for classes of
course.
 
V

Victor Bazarov

call_me_anything said:
I have different kind of data structures in different applications.
The data structures are big and complex and I would like to print the
members of each struct.

Why? How do you want to print them? Are there any pointers in your
structs? How do you intend to print the pointers? Should they be
automatically dereferenced?
Can we write a generic piece of code which will
automatically find out the type of struct elements and
print them (indented manner ?) if they are primitive types like
int, float or char* ? else it will recurse into the composite data
type.

Not that I know of. However, the implementation of the operator<<
is extremely simple.
This is to avoid writing such functions for each struct in each
application.

But usually you don't need to write such functions for _each struct_
in each application. You only need to write those functions for
the structs you *intend to output*.
Sounds something close to gdb ?

With 'gdb' (and other debuggers) there is "debugging information"
that actually doesn't exist in C++ (no such concept is defined).
So, if you want to be OS- or linker-specific, you're welcome to
write your own implementation using the format of the "debugging
information" available.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top