Can I call constructor explicitly, without using new, if I already have a memory for object?
class Object1{
char *str;
public:
Object1(c
Let me show you some code on how it can be done, both in construction and destruction
#include
// Let's create some memory where we will construct the object.
MyObject* obj = (MyObject*)malloc(sizeof(MyObject));
// Let's construct the object using the placement new
new(obj) MyObject();
// Let's destruct it now
obj->~MyObject();
// Let's release the memory we used before
free(obj);
obj = 0;
I hope the above summary makes things clearer.