I came a across a new problem when using templates. This is me being a little creative from a book I recently read, expanding his ideas, and it has this code example.
Say you have a templated Array2D class. And you have this method (Array2D::WriteFile):
bool WriteFile( const char* p_filename )
{
FILE* outfile = 0;
int written = 0;
// open the file
outfile = fopen( p_filename, "wb" );
// return if it couldn't be opened
if( outfile == 0 )
return false;
// write the array and close thef ile
written = fwrite( m_array, sizeof( Datatype ), m_size, outfile );
fclose( outfile );
// if we didn't write the number of items we expected,
// return failure
if( written != m_size )
return false;
// return success.
return true;
}
This works fine for basic data types such as int, char, float, etc. But what if you get a little creative making the data type a compound variable such as SLinkedList (singly linked list). It would be Array2D. Then you call your old WriteFile() function. It’ll save it, but you just only saved one node. The rest of the nodes went into oblivion never to return.
A few ideas came in my head:
1. Twiddle your thumbs watching the clock go by towards your deadline.
2. Make Array2D::WriteFile() a pure virtual function causing a more specific class to save it the way it should. But then I can’t use Array2D by itself in other stand-alone situations.
3. Write a function pointer to save, but I think it could get messy because you don’t know what data your passing into til the time comes. It could vary based on the data type.
4. Realize templates aren’t the solution, perhaps.
The template class isn’t solving every case scenario for me based on the data type. So what would be a good solution in your opinion? Thanks!
Your objects need to be serializable. Think about what actually happens under the hood when you try to write out the head of a linked list this way – of course it just writes the first node; the
writefunction has no idea what it’s writing, so it has no idea how to follow the pointers to the next node and write that too. It just sees bits and writes them out until the end of the current object, which is in that case just the first node.Serialization basically means, for each non-trivial type, writing a method that packages up that type into a flat stream of bytes suitable for writing out (and ideally also a second method that reads that format back in).
Now, your question is tagged C++, but the code you’ve written is very C-like. The normal way to implement a serializable object in C++ is to override the
<<operator with astd::ostream&argument and return value, e.g.And symmetrically
Now, you can just do something like this:
Note that this approach of overriding the stream operators means that if, for example
MyType::field2happens to itself be a complex object, it will still be serialized and deserialized properly in the above code as long as its stream operators are overridden.But if you want to continue using C-style file I/O as in your original post, things aren’t going to be quite so clean. If your code is really supposed to be C++ though, you should be using the iostream libraries to do your file I/O.