The most common way of creating an object of a class is by using the new
keyword. It also calls the constructor.
But if we used the malloc
function to create the object, the constructor doesn't get called. Is it still possible to manually call the constructor after creating the object using malloc
?
It sounds like you want to call the constructor an a piece of memory created by malloc
. This is possible and is called placement new
void* pMemory = malloc(sizeof(C));
C* pValue = new (pMemory) C();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With