Home  /  Questions  /  Question



50   50
Sep 09, 2014


Virtual + Template Method pattern: brilliant solution needed

I have the same implementation for 2 methods, they both insert argument to a text file.

void WriteToFile( double content) //for double
{    
streamFile << content;    
streamFile << flush;
}

void WriteToFile( int content) //for integer
{    
streamFile << content;    
streamFile << flush;
}  

The implementation is same, therefore I merge them to a template method:

template < class T>
void WriteToFile(T content)
{    
streamFile << content;    
streamFile << flush;
}  

But the WriteToFile() method should be virtual.Is there a design-pattern expert's trick to deal with it?