January 2020
Intermediate to advanced
454 pages
11h 25m
English
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 ...