Assume the following dummy template:
template < class DataType > class Dummy
{
public:
void init( )
{
m_data = DataType( 0 );
}
private:
DataType m_data;
};
Calling init will init the internal data. This does work fine when DataType is a standard data type (e.g. int or float). When DataType is a class this class must have a corresponding constructor.
Now assume DataType shall be e.g. a Complex Number represented by a suitable class. In this case it does not make sense to give the Complex number class a constructor with one argument because under normal conditions you want initialize real and imaginary part.
So my question is:
What is the best generic way to initialize a template type under consideration that the template shall be suitable to store any data type.
I think e.g. the STL must implement thinks like this but I am lost within that code.
In your example I guess you meant:
Not:
In any case, try:
That will call the default constructor for a class type, or will zero-init for a built-in type.