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

Get Expert C++ Programming now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.