O'Reilly logo

C++17 STL Cookbook by Jacek Galowicz

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

How to do it...

Let's implement a program that knows the types, cat and dog, and that stores a mixed list of cats and dogs without using any runtime polymorphy:

  1. First, we include all the needed headers and define that we use the std namespace:
      #include <iostream>      #include <variant>      #include <list>      #include <string>      #include <algorithm>            using namespace std;
  1. Next, we implement two classes that have similar functionality but are not related to each other in any other way, in contrast to classes that, for example, inherit from the same interface or a similar interface. The first class is cat. A cat object has a name and can say meow:
      class cat {          string name;      public:          cat(string n) : name{n} {}
                void meow() const { cout << name << " ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required