close
Template class is similar to function template ,
We can use the template to instead of type of variable or method of class.
For example :
using
namespace
std;
template
<
typename
T>
class
Storage
{
private
:
T m_tValue;
public
:
Storage(T tValue)
{
m_tValue = tValue;
}
~Storage()
{
}
void
Print()
{
std::cout << m_tValue << std::endl;;
}
};
int
main()
{
// Define some storage units
Storage<
int
> nValue(5);
Storage<
double
> dValue(6.7);
// Print out some values
nValue.Print();
dValue.Print();
}
全站熱搜