Skip to Content
Advanced C++ Programming Cookbook
book

Advanced C++ Programming Cookbook

by Dr. Rian Quinn
January 2020
Intermediate to advanced
454 pages
11h 25m
English
Packt Publishing
Content preview from Advanced C++ Programming Cookbook

How it works...

When we typically think of C++ type erasure, this is the example we think of. The type erasure pattern is needed when we must leverage a set of objects as if they are related, that may or may not share a common base class (that is, they either do not use inheritance or if they do use inheritance, it is possible they do not inherit from the same set of classes).

For example, suppose we have the following classes:

class spiderman{public:    bool attack(int x, int) const    {        return x == 0 ? true : false;    }};class captain_america{public:    bool attack(int, int y) const    {        return y == 0 ? true : false;    }};

As shown in the preceding code snippet, each class defines a different type of hero. We would like to do something like this:

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

Modern C++ Programming Cookbook

Modern C++ Programming Cookbook

Marius Bancila

Publisher Resources

ISBN: 9781838559915Supplemental Content