Can't declare a class without initialization?

For standard data objects, such as int, you can do the following:

int number; number = 0; 

Basically, you can declare a number before initializing it, useful if you initialize inside various if statements, and you don't want the number to go outside the scope.

Is it possible to do something similar with custom classes?

I have a class called mem_array with a form constructor

  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 the scope of if statements. Could this be done?

+6
source share
7 answers

Your first line will give you an error, since the constructor has no default values, and the constructor without parameters does not exist.

Just use a pointer (or even better, a smart pointer, so you don't have to worry about deleting the object). But don't forget to check later that x was either 0 or 1, i.e. check that myData is constructed.

 mem_array* myData=0; if(x==0) myData=new mem_array(1,1,"up"); if(x==1) myData=new mem_array(0,0,"down); assert(myData!=0); 
+4
source

add constructor to mem_array that accepts int

so you can declare / use ...

mem_array myData (x);

inside this constructor, add the initialization / condition code that you want to use.

+2
source

You can use a pointer:

 unique_ptr<mem_array> myData; switch (x) { case 0: myData.reset(new mem_array(1, 1, "up")); break; case 1: myData.reset(new mem_array(0, 0, "down"));; break; } 
+1
source
 int number; number = 0; 

This last line is explicitly number = int(0); . In other words, use a type:

 mem_array myData; if (x == 0) myData = mem_array(1, 1, "up"); if (x == 1) myData = mem_array(0, 0, "down"); 

This, unfortunately, requires operator = () overloading:

 class mem_array { ... public: mem_array& operator= (const mem_array& cma); { /* Copy the information from cma to *this. */ /* Return a reference to this object. */ return *this; } }; 

An alternative is to use pointers (dynamic allocation), as others have recommended. It is up to you which you use at the end.

+1
source

Such a declaration automatically calls the default constructor (i.e., with no argument or with all arguments, it has a default value). The compiler will create it automatically if your class does not have a constructor, but since you have one, it will not be created automatically. Therefore, the declaration is not executed because it cannot find the default constructor.

0
source

Operator ?: Underestimated.

 mem_array myData = (x==1) ? myData(1,1,"up") : myData(0,0,"down"); 
0
source

Another solution is to use boost::optional<T> . This makes it explicit that your variable may contain a not-aT value. For instance.

 boost::optional<mem_array> myData; // Doesn't hold a mem_array yet. if(x==0) myData = mem_array(1,1,"up"); if(x==1) myData = mem_array(0,0,"down"); 
0
source

Source: https://habr.com/ru/post/891778/


All Articles