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....

Recommended Answers

All 4 Replies

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 ...

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)

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

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.