I want to make a class factory that can create classes knowing nothing about them (eg no headers included). I created a factory class with map, that stores info about classes (sizeof and pointer to static method) by strings ( map(string, mystruct) ), which is filled from each class.
When factory is producing a class, it allocates raw memory and calls static method, which then calls the default constructor on that memory:
void *ptr = operator new(classSize); classCCaller(ptr);//void (*classCCaller)(void*)
then inside the static method of that class the following code is executed:
new (ptr) SomeClassConstructor();
The produced class is then put in some container.
Question - is it ok to delete classes, created that way, by
delete p;//SomeClass *p;
or should I manually call destructor and use operator delete on untyped pointer?
No, you have to say
p->~SomeClass(); operator delete(ptr);, where
p = static_cast<SomeClass*>(ptr).