Dump all Private Data Member to File

Hello all,

I have a poorly written class which has about 300+ private data members.

I would like to know if there is a method that already exists somewhere within c++ that will take all the private data members and write them to a file.

Since I am a researcher and I am analyzing data, my code will never be implemented for public use, so for the sake of progress I would not like to spend much time on making my code look and work elegant. I just need to progress.

With that said, it is a pain for me to write each of the 300+ members by hand. ie. fHistogramPt->Write();

If I simply write the whole class I have to a file, then I have to ensure that I have written 300+ unique getters to access the private members.

I could always just make all the private data members public, and write the whole class to the file. But even I have some basic rules I do not like to break.

I was thinking of throwing all my private data members into a list instead and just saving the list. I've never bothered with lists before, can I put differnt objects in a list?

In case this helps: I have ensured that each data member has a unique name and title. So if lists were able to allow searching, I could search for the name when I want to retrieve the object, instead of remembering which index belongs to which object.

I hope this was clear enough. I just reread this post and it is indeed confusing, but it is a confusing matter.

Cheers,
Bo
Wait, are you trying to print out the data inside an object, or are you trying to print out the members of the class? A distinction must be drawn.
Be wary of this: http://en.wikipedia.org/wiki/God_object
Last edited on
So, if I understand correctly you have something like this :

1
2
3
4
5
6
7
8
9
class poorly_written_class {
public:
    void print(); //for printing
private:
    int i1;
    int i2;
    //..
    int i300;
};

Are the types of the data members the same? Are their names have a pattern like mine?
The reason I ask because if they have some sort of similarity, then you can easily generate code, which would print the data out.

1
2
3
void poorly_written_class::print() {
    file << i1 << ' ' << i2 << ' ' << /*...*/ << i300; //See the pattern?
}


If the members have different types and/or names, then the generator *can* get a little more complicated.
Please post some example code, so we can get a more specific solution for you.
I'm sorry about my jargon.
I have never taken a computing class, so I sling out the wrong words all the time.

I am trying to write only the private data members of a class to a file.
For completeness here is a quick mock up of my class:
class class_name {
public:
member1;
private:
member2;
...
member367;
} object_names;

I would like to just write member 2 thorugh 367 to a file simply.

I've known about the God object syndrome. I still catch myself in that situation once in a while, but this is luckily not one of them
Or better, pastebin it, because I don't want to look at 300 lines of member variables.
Hi Romal,
YOu must have submitted when I was replying to tummychow.
Yes you are correct.

Yes, and of course the answer was easy. It is so simple I didnt think of even just using the "<<" operators.

My datamembers are named odd things but I will figure out how to stream line this.

Thanks.

Problem solved!
Couldn't you make the members from 2 to 367 into a union?

Etc:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
#include <fstream>

struct member
{
	int ID;
	int age;
};

class class_name
{
	public:
		class_name()
		{
			one.age		= 25;
			one.ID		= 1;
			two.age		= 32;
			two.ID		= 2;
			three.age	= 12;
			three.ID	= 3;
			four.age	= 67;
			four.ID		= 4;
			five.age	= 17;
			five.ID		= 5;
		}
		~class_name()
		{
		}

		member one;

		void DisplayMembers()
		{
			std::cout << "Member 1:\n";
			std::cout << "\tAGE: " << one.age << "\n";
			std::cout << "\tID : " << one.ID  << "\n";
			for( int i = 0; i < 4; i++ )
			{
				std::cout << "Member " << (i + 2) << ":\n";
				std::cout << "\tAGE: " << allmembers[i].age << "\n";
				std::cout << "\tID : " << allmembers[i].ID  << "\n";
			}
		}

		void PrintMembers()
		{
			std::ofstream outfile("Members.txt");
			for( int i = 0; i < 4; i++ )
			{
				outfile << "Member " << (i + 2) << ":\n";
				outfile << "\tAGE: " << allmembers[i].age << "\n";
				outfile << "\tID : " << allmembers[i].ID  << "\n";
			}
			outfile.close();
		}
	private:
		union
		{
			member allmembers[4];
			struct
			{
				member two;
				member three;
				member four;
				member five;
			};
		};
} object_names;

int main()
{
	object_names.DisplayMembers();
	object_names.PrintMembers();

	return 0;
}
Topic archived. No new replies allowed.