By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,369 Members | 1,188 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,369 IT Pros & Developers. It's quick & easy.

save: reflection?

P: n/a
hello all,

in my application, I want to save the state of a number of classes to disk: what would be the best way to do this?

right now, I'm hard coding the fields of the classes, but of course after
every change to the classes, I also have to adapt the 'save' method. Is there a better way, perhaps using some kind of reflection?

I've searched boost.org for reflection, but there don't seem to be ready made classes?

thanks for any answer,
Danny.
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Sat, 11 Dec 2004 21:43:11 +0100, danny van elsen
<da*************@hotmail.com> wrote:
hello all,

in my application, I want to save the state of a number of classes to disk: what would be the best way to do this?

right now, I'm hard coding the fields of the classes, but of course after
every change to the classes, I also have to adapt the 'save' method. Is there a better way, perhaps using some kind of reflection?

I've searched boost.org for reflection, but there don't seem to be ready made classes?


One approach is to only ever have one data member, a boost::tuple.
That's not nice though, since you have to add a load of private
accessor methods really to avoid nasty syntax (and you may need
const/non-const overloads). Alternatively, accept the small
maintenance overhead (which does give you backwards compatibility and
versioning, unlike if you use reflection directly as is often done in
Java), and use the boost serialization library.

Tom
Jul 22 '05 #2

P: n/a

danny van elsen wrote:
hello all,

in my application, I want to save the state of a number of classes to
disk: what would be the best way to do this?
Are they PODs?
right now, I'm hard coding the fields of the classes, but of course after every change to the classes, I also have to adapt the 'save' method.
Is there a better way, perhaps using some kind of reflection?


How do you deal with load(), especially with loading older files?
There is a common notion that classes related to file structures
should not change, period. If you need another field, create
a new C++ type and a new file format.

Another solution is to use an XSD to C++ compiler. If you want a new
field, change the XSD and recompile it. They usually will generate
the "save to disk" funcions for you as well.
In the same vein you could use an O/R mapper to save objects to a DB.
Regards,
Michiel Salters

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.