From time to time I come across the following (or something like that) class hierarchy in different C++ projects:
class DummyBase
{
public:
virtual ~DummyBase() {}
virtual void doSomething() = 0;
};
template<typename T>
class Dummy : public DummyBase
{
public:
void doSomething() override
{
...
}
private:
T field;
};
Looks like as C++ idiom, pattern or maybe trick. Can you tell me the purpose of this contruction and what's the problem can be solved using it. I would be glad to read good articles or maybe books that describe it.
This is polymorphism based type erasure, it is often used for handling things like std::function
or std::any
which can store data of unrelated types in the exact same manner (e.g. function pointers and member function pointers for std::function
).