// @topic T53205 12/04/2012 -- class template, function template demo
// @brief template class Box, template function max_val( )
#include <string>
// Example of a class template --
// Box is a placeholder of some data:
template< typename ValueType >
class Box {
ValueType m_value;
public:
// conversion constructor
Box<ValueType>( ValueType value )
:
m_value( value )
{
}
// copy constructor
Box<ValueType>( Box<ValueType> const& other )
:
m_value( other.m_value )
{
}
// operations
ValueType get_value() {
return m_value;
}
ValueType get_default_value() {
return ValueType();
}
void set_value( ValueType value );
};//class Box
// Example of a member function of the class template:
template< typename ValueType >
void Box< ValueType >::set_value( ValueType value ) {
m_value = value;
}
// Example of a function template --
// Returns bigger ValueT
template< typename ValueT >
ValueT const& max_val( ValueT const& a, ValueT const& b )
{
return ( a > b ) ? a : b;
}
int main()
{
std::string str = "hello";
Box< double > dbl_bx = 123.45;
double dbl = dbl_bx.get_value();
dbl_bx.get_default_value();
Box< std::string > str_bx = "";
str_bx.set_value( str );
Box< std::string > str_bx2 = str_bx;
Box< int > int_bx = 789;
int xx = int_bx.get_value();
int max = max_val( 100, 200 );
return 0;
}