0

I want to allocate an array of C++ objects using the following code:

class myClass {
public:
    myClass(int userValue)
    : value(userValue)
    { }
}

private:
    int value;
};

int main(){
    myClass* objArray = new myClass(22)[5];

    return 0;
}

But it gives me the following error:

In constructor ‘myClass::myClass(int32)’:
error: expected ‘;’ before ‘[’ token
         objArray = new objArray(22)[5];

How should I create an array of objects then while passing parameters to them?

Barry
  • 286,269
  • 29
  • 621
  • 977
Arya Mz
  • 581
  • 3
  • 7
  • 20

1 Answers1

2

Use std::vector.

std::vector<myClass> objArray(5, 22);
Columbo
  • 60,038
  • 8
  • 155
  • 203