So I was reading this article about type erasure. But the code in that article seems partially incorrect, for example:
template <typename T>
class AnimalWrapper : public MyAnimal
{
const T &m_animal;
public:
AnimalWrapper(const T &animal)
: m_animal(animal)
{ }
const char *see() const { return m_animal.see(); }
const char *say() const { return m_animal.say(); }
};
followed by
void pullTheString()
{
MyAnimal *animals[] =
{
new AnimalWrapper(Cow()), /* oO , isn't template argument missing? */
....
};
}
These mistakes discouraged me from reading any further in the article.
Anyways; can anyone please teach what type erasure in C++ means, with simple examples?
I wanted to learn about it to understand how std::function
works, but couldn't get my head around it.