In this lesson we deal with the concept of using std::vector and std::variant in Polymorphism. Interesting? Read on to find out more!

We'll cover the following

Most of the time in C++, we can safely use runtime polymorphism based on a vtable approach. You have a collection of related types that share the same interface, and you have a well defined virtual method that can be invoked.

But what if you have “unrelated” types that don’t share the same base class? What if you’d like to quickly add new functionality without changing the code of the supported types?


With std::variant and std::visit we can build the following example:

Get hands-on with 1200+ tech skills courses.