0

Hey, im trying to delete an element of an array of pointers of a class, how can i delete just a single element?
my code is basically this...

n =8;

ClassName *newName;
newName = new ClassName[n];

how would i delete for example, element 3, while being in that element... if that makes sense, at the moment ive just tried

delete newName;

and it compiles but prints out alot of errors when running it....

3
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by mvmalderen
0

I don't think it's possible in your way, you should declare an array of pointers instead if you want to delete individual elements ...

0

I don't think it's possible in your way, you should declare an array of pointers instead if you want to delete individual elements ...

Edit::

int n = 5;
Classname ** ptr_to_array = new Classname * [n];

Now you should use a for loop to assign objects to the pointers ...
(You should also use a for-loop to destruct it)

0

Tux4life is right, you can't delete one element of an array in C(++). Either use a vector or do as tux4life said.

0

Tux4life is right, you can't delete one element of an array in C(++). Either use a vector or do as tux4life said.

Yeah, a vector is also a very good solution :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.