"Philipp" <_N******************@hotmail.com> wrote in message
news:3f********@epflnews.epfl.ch...
Sorry my code was wrong... Is this correct?
<code>
int N = 22;
pointerArray* MyClass;
pointerArray = new MyClass[N];
for (int i=0; i< N; i++)
pointerArray[i]->MyClass(i);
</code>
No. It would be much easier if you had a default constructor. Then you
could just declare an array of objects instead of pointers. But to create
an array of pointers, you first need to declare the array, then create
instances of the objects for each array item to point to. You can't just
call a constructor like a function...you have to "new" each pointer, like
this:
int N = 22;
pointerArray* MyClass[N]; // no need to "new" this!
for (int i = 0; i < N; ++i)
pointerArray[i] = new MyClass(i); // create each instance!
....and, later, to delete...
for (int i = (N-1); i >= 0; --i)
delete pointerArray[i]; // delete each instance
(BTW, you could count upwards in the delete loop, I just got in the practice
long ago of deleting in the opposite order I allocated in, because on some
systems it prevented memory fragmentation...but that's just me.)
-Howard