In C++, I want to define an object as a member of a class like this:
Object myObject;
However doing this will try to call it\'s parameterle
You can fully control the object construction and destruction by this trick:
template
struct DefferedObject
{
DefferedObject(){}
~DefferedObject(){ value.~T(); }
template
void Construct(TArgs&&...args)
{
new (&value) T(std::forward(args)...);
}
public:
union
{
T value;
};
};
Apply on your sample:
class Program
{
public:
DefferedObject
Big advantage of this solution, is that it does not require any additional allocations, and object memory allocated as normal, but you have control when call to constructor.
Another sample link