Skip to Content
Hands-On Design Patterns with C++
book

Hands-On Design Patterns with C++

by Fedor G. Pikus
January 2019
Intermediate to advanced
512 pages
14h 5m
English
Packt Publishing
Content preview from Hands-On Design Patterns with C++

Class templates

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 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Structural Design Patterns in Modern C++

Structural Design Patterns in Modern C++

Umar Lone

Publisher Resources

ISBN: 9781788832564Supplemental Content