Can you please tell me if it is possible to call object constructor manually? I know it's wrong and I would never do something like that in my own code and I know I can fix this problem by creating and calling initialization function, however the problem is that I stumbled at a case where there are thousands of lines of code in object's and its parents' constructors...
class MyClass()
{
MyClass() { }
virtual ~MyClass();
void reset()
{
this->~MyClass();
this->MyClass::MyClass(); //error: Invalid use of MyClass
}
};
A constructor is called using placement new
This constructs a MyClass in an empty space at address.
Would never do this inside the class though!
Edit
If you already have an object of the right type, and want to assign it default values, an alternative is
which creates a new object with default values and assigns that value to your existing object.
You could use placement new syntax:
You can still move construction/destruction into separate functions and call those directly. i.e.