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
  • 267,863
  • 28
  • 545
  • 906
Arya Mz
  • 581
  • 3
  • 7
  • 19

1 Answers1

2

Use std::vector.

std::vector<myClass> objArray(5, 22);
Columbo
  • 58,324
  • 8
  • 149
  • 196