I have two constructors for an objects, which use two different sets of data.
myclass myobject(set 1);
myclass myobject(set 2);
set 1 and set 2 are of different types.
I want to create an object of myclass called myobject either with constructor 1 or constructor 2 depending on the set of arguments are available at this point and then use myobject outside of the if statement.
if(set1)
myclass myobject(set 1);
else
myclass myobject(set 2);
myobject.operation();
Can anyone tell me the good way to do this?
If default construction and copy (or move) assignment are cheap, just use:
If it is acceptable to allocate things on the heap, use a pointer:
If you must allocate it on stack, I don’t see anyway except using placement new (or changing the constructor to accept both possibilities).