January 2019
Intermediate to advanced
512 pages
14h 5m
English
Instantiation of class templates is similar to that of the function templates—the use of the template to create a type implicitly instantiates the template. To use a class template, we need to specify the type arguments for the template parameters:
template <typename N, typename D>class Ratio { public: Ratio() : num_(), denom_() {} Ratio(const N& num, const D& denom) : num_(num), denom_(denom) {} explicit operator double() const { return double(num_)/double(denom_); } private: N num_; D denom_;};Ratio<int, double> r;
The definition of the r1 variable implicitly instantiates the Ratio class template for the int and double types. It also instantiates the default constructor of this class. The second constructor is not used ...