944,124 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6785
  • C++ RSS
Jun 27th, 2005
0

Dynamically Print Out Structure Contents

Expand Post »
Hopefully I can ask this clearly. Basically I'm wondering if I have a structure like so:

struct MyStruct
{
char fieldA[20];
char anotherField[15];
int andAnother;
char lastField[30];
};

Is there a way to dynamically print out it's contents?

[fieldA] = "ABCDE"
[anotherField] = "TEST"
[andAnother] = 5
[lastField] = "WHATEVER"

With the capability of sending it a completely different structure (different fields, different sizes, different number of fields, etc.) and it could do the same. I know this is possible in Java using reflection, etc. but wondering if there is a C++ alternative (or close approximation). Note that I would be satisfied even if it required some sort of precompilation for it to work...
Similar Threads
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Jun 27th, 2005
0

Re: Dynamically Print Out Structure Contents

You basically want a print struct function? (That works for any struct)

Sounds like you don't want a struct at all but rather a class. Or rather a set of classes that all inherit from a base class that has a virtual print() function. Then all the child classes would have to define a print class that printed their data and instantiate the function.

That would be a sensible way of doing it.

A completly insane way of doing it would be to make a struct Type that contains:
* Data type
* Data length
* Data

Then make a set of structs that contain
* A counter of how many variables are in this struct
* An array of pointers to the Types

You can now code something that takes one of your structs, looks up how many data items are in it, accesses them via the area, when it accesses them it findout what type they are and if needed how big they are, then it uses the correct print method to show the output.

Avoid doing this if you can? (There's definitly a well researched way of doing it as exactly this is needed in order to get from C to C++ which is written in C - but why redo the work???)
Reputation Points: 10
Solved Threads: 1
Newbie Poster
oboler is offline Offline
13 posts
since Jun 2005
Jun 27th, 2005
0

Re: Dynamically Print Out Structure Contents

That's the thing - I don't want to have to write a separate print function for each class. I basically want to have the ability to get information about the fields of the class without knowing anything about it. (in the same way I can do typeinfo( whatever).name(). (I think what I'm saying is yes, I want the insane way!) This way I can code it once and send any struct to it.
Reputation Points: 68
Solved Threads: 18
Posting Pro in Training
winbatch is offline Offline
466 posts
since Feb 2005
Jun 28th, 2005
0

Re: Dynamically Print Out Structure Contents

I hope you need this for a good reason... it's not the most efficient way of doing things, or the easiest code from someone else to then understand. In the long run I think you're better off not using such a function unless there is a very good reason for it.

Anyway best of luck.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
oboler is offline Offline
13 posts
since Jun 2005
Jun 28th, 2005
0

Re: Dynamically Print Out Structure Contents

Maybe what you really want is a different programming language.
Team Colleague
Reputation Points: 1135
Solved Threads: 173
Super Senior Demiposter
Rashakil Fol is offline Offline
2,479 posts
since Jun 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ Tic Tac Toe using classes & operator overloading
Next Thread in C++ Forum Timeline: need help overloading =, >>, <<





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC