A
Andy Gibbs
Hello!
Please could someone help me with what is probably a trivial problem, but is
definitely tying me in knots!
I have a class MyObject, and I need to create a pointer to an array of
pointers to this class. I think I am correct with the definition of...
MyObject* (*pointer)[];
which I take to mean a pointer to an array of MyObject pointers.
I believe I am also correct with my accessor function:
MyObject& item(int index) {
return *((*pointer)[index]);
}
However, I am having real difficulty working out how to use new and delete
with my pointer. So the following doesn't work:
pointer = new MyObject*[size];
for (int i = size; i-->0
(*pointer) = new MyObject(...);
Because the first line fails compile with "cannot convert DbString** to
DbString*(*)[] in assignment". (I am using GCC 4.1.2.)
The following use of delete compiles, but I am not sure it does what I think
(hope?) it does!
for (int i = size; i-->0
delete (*pointer);
delete[] pointer; // should this be delete[] *pointer ?
Please, any help will be very gratefully appreciated!
Andy.
Please could someone help me with what is probably a trivial problem, but is
definitely tying me in knots!
I have a class MyObject, and I need to create a pointer to an array of
pointers to this class. I think I am correct with the definition of...
MyObject* (*pointer)[];
which I take to mean a pointer to an array of MyObject pointers.
I believe I am also correct with my accessor function:
MyObject& item(int index) {
return *((*pointer)[index]);
}
However, I am having real difficulty working out how to use new and delete
with my pointer. So the following doesn't work:
pointer = new MyObject*[size];
for (int i = size; i-->0
(*pointer) = new MyObject(...);
Because the first line fails compile with "cannot convert DbString** to
DbString*(*)[] in assignment". (I am using GCC 4.1.2.)
The following use of delete compiles, but I am not sure it does what I think
(hope?) it does!
for (int i = size; i-->0
delete (*pointer);
delete[] pointer; // should this be delete[] *pointer ?
Please, any help will be very gratefully appreciated!
Andy.