Saving/Loading a class instance to file

P

Peter Oliphant

Trying to save/load a class instance to a file is tough. The reason is
because there is no way to preserve its type other than by conventon. For
eample, one can save all the states of the members of a class instance to a
file. But when loading it up the program can only read in the values stored.
It has no idea what kind of class they came from, and there is no way to
save info (that I'm aware of) that let can allow the application to figure
this out at runtime. The programmer must therefore figure out a saving
convention and then load the approriate type according to his convention.

This is especially a problem with templated classes. This is because an
instance of a templated class requires it be made concrete at compile time.
It is therefore impossible to write a Save_To_File() member method for a
templated class, since one can't override it for different types, and there
is no way of constructing an arbitray template class (i.e., one where the
template arguments are variables).

Anybody have clever ways of saving a class instance in such a way it can be
restored without knowing ahead of time what class it came from in the first
place? For example, can I save something to a file that when loaded can let
me build at runtime, say, an arbitrary class or data type?

[==P==]
 
B

Brian Muth

Sounds to me you are trying to reinvent what is already prescribed and
available. Take a look at the IPersist* family of interfaces.

Brian
 
P

Peter Oliphant

Thanks Brian!

Indeed, I'll look this up. So far the MSDN2 does list IPersist, but doesn't
really describe it, it just --- IS. I also see I should study IMoniker. If
you know of any sources of sample code, preferably in /clr managed VS
C++.NET (Express), that would be great too... : )

[==P==]
 
B

Brian Muth

The best reference for IMoniker is "Inside OLE" by Kraig Brockshmidt. The
entire text of this book is available on one of the older MSDN Library
CD's... I forget which one.

Brian
 

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

Top