For standard data objects such like int, the following can be done
int number;
number = 0;
Basically, you can declare number before initializing it, useful if you initialize inside various if statements and you don’t want number going out of scope.
Can something similar be done with custom classes?
I have a class called mem_array with constructor of the form
mem_array(int,int,std::string);
I would like to do the following
mem_array myData;
if(x==0) myData(1,1,"up");
if(x==1) myData(0,0,"down");
basically, so I can use myData outside of the scope of the if statements. Can something like this be done?
Your first line will give you an error since the constructor doesnt have default values and a constructor without parameters doesnt exist.
Just use a pointer (or even better a smart pointer, so you dont have to take care of deleting the object). But be sure to check afterwards that x was either 0 or 1, i.e. check that myData has been constructed.