I have some third-party abstract base class
struct foo
{
virtual foo*job() = 0;
...
static void* make_space(size_t sizeofDerived);
};
which I cannot alter. Objects of type foo
(and all derived classes) must only be created/constructed using placement-new into the memory returned by foo::make_space()
,
because ordinary construction, as in
derived_from_foo z; // or
auto p = new derived_from_foo();
may cause undesired problems (corrupting memory, depending on the compiler, as I found out eventually). So my question: how can I write/design a derived class
struct bar // still abstract, since foo::job() is not provided
: foo
{
...
template<typename Derived, typename...Args>
static Derived* create(Args&&...args)
{
return ::new(foo::make_space(sizeof(Derived)))
Derived(std::forward<Args>(args)...);
}
};
such that construction of objects of type bar
or any type derived from bar
by any other means than through bar::create()
fails at (i) compilation or (ii, less ideal) at run-time?