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

Discussion in 'C++' started by call_me_anything, Sep 30, 2007.

  1. 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 ?
     
    call_me_anything, Sep 30, 2007
    #1
    1. Advertising

  2. 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.
     
    call_me_anything, Sep 30, 2007
    #2
    1. Advertising

  3. Re: print struct fields and its member structs' fields recursively,generically

    On 2007-09-30 16:38, call_me_anything wrote:
    > 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.

    --
    Erik Wikström
     
    =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=, Sep 30, 2007
    #3
  4. call_me_anything wrote:
    > 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Sep 30, 2007
    #4
  5. call_me_anything

    Pete Becker Guest

    On 2007-09-30 04:38:30 -1000, call_me_anything <> said:

    > Java probably has a toString() method defined automatically for every
    > object that
    > does such a kind of job.


    No, it doesn't.

    --
    Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
    Standard C++ Library Extensions: a Tutorial and Reference
    (www.petebecker.com/tr1book)
     
    Pete Becker, Sep 30, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Patricia  Van Hise

    structs with fields that are structs

    Patricia Van Hise, Apr 5, 2004, in forum: C Programming
    Replies:
    5
    Views:
    667
    Al Bowers
    Apr 5, 2004
  2. pete142
    Replies:
    5
    Views:
    466
    Ben Bacarisse
    Jan 23, 2008
  3. Replies:
    3
    Views:
    314
  4. thunk
    Replies:
    1
    Views:
    364
    thunk
    Mar 30, 2010
  5. Tuan  Bui
    Replies:
    14
    Views:
    531
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page