Data Structures: Ditto::static_ptr<Base, Derived, ...>
One of the nice things about C++
compared to C
is its ability to define reusable types and data structures. They make code reuse easier and also help with reasoning if the abstraction is high-level enough.
Today we are going to talk about static_ptr
from the library Ditto. Dynamic allocation is often forbidden when developing embedded systems. This leads to allocating most things either in the stack or globally. A static_ptr
allows the user to statically allocate an object of a derived class and access it as a base class pointer. The nice thing is that it allows to easily implement the factory pattern
if only one instance of each child is required at a time.