is it possible to re-initialize an object of a class using its constructor?
In C++11, you can do this:
#include
template
void Reconstruct(T& x, Args&&... args)
{
static_assert(!std::has_virtual_destructor::value, "Unsafe");
x.~T();
new (&x) T(std::forward(args)...);
}
This allows you to use Reconstruct passing arbitrary constructor parameters to any object. This can avoid having to maintain a bunch of Clear methods, and bugs that can easily go unnoticed if at some point the object changes, and the Clear method no longer matches the constructor.
The above will work fine in most contexts, but fail horribly if the reference is to a base within a derived object that has a virtual destructor. For this reason, the above implementation prevents use with objects that have a virtual destructor.