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!