Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Allocating an array of a class c++

Tags:

c++

How would I go about allocating an array of a class without constructing the class, so I could fill up the array later?

I was originally trying to use

Myclass * array = new Myclass[N];

But it tries to construct Myclass to N.

like image 818
Fox-3 Avatar asked Dec 06 '25 20:12

Fox-3


1 Answers

First just declare it without allocating

Myclass * array[N]; 

when you need it

for(int i=0;i<N;i++){
 array[i] = new Myclass(/*params*/);
}

But consider using std::vector/std::list if you must not have to manage memory yourself.

like image 72
Rakib Avatar answered Dec 08 '25 10:12

Rakib



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!