user788171
user788171

Reputation: 17553

unable to declare class without initialization?

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?

Upvotes: 9

Views: 5960

Answers (7)

MSalters
MSalters

Reputation: 180245

Another solution is to use boost::optional<T>. This makes it explicit that your variable can hold the value not-a-T. E.g.

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");

Upvotes: 0

MSalters
MSalters

Reputation: 180245

The ?: operator is underrated.

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

Upvotes: 1

user539810
user539810

Reputation:

int number;
number = 0;

That last line explicitly is number = int(0);. In other words, use the 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 you to overload operator=():

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's up to you which you use in the end.

Upvotes: 1

MessyHack
MessyHack

Reputation: 133

add a constructor to mem_array that takes an int

so that you can declare/use...

mem_array myData(x);

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

Upvotes: 2

LeleDumbo
LeleDumbo

Reputation: 9340

Declaration like that automatically calls the default constructor (i.e. with no argument or with all arguments have default value). The compiler would create it automatically when your class doesn't have any constructor, but since you have one, then it won't be created automatically. Therefore, the declaration fails because it can't find the default constructor.

Upvotes: 0

C. K. Young
C. K. Young

Reputation: 223183

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;
}

Upvotes: 1

pokey909
pokey909

Reputation: 1837

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.

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);

Upvotes: 4

Related Questions