0

Overloading the operator -=:

RentACar& operator-=(const Automobile& av)

In RentACar i have a dynamically allocated array of Automobile *a

So lets say that if any a has the same a.brand with av.brand then that a should be removed and the allocated memory freed for one object.

I thought sth. like if it doesnt find an object with the same brand it return *this, but if it does, it allocates space for a new automobile array of size of a-1, assigns objects of, skips the one with the same brand, but i dont know how to write it.

I don't know how to write with vectors and push back, anyway, i study for an exam and we never used vectors for whatever reason so i can't use them on the exam so i must do this even if it is a stupid implementation!

Thanks!

Stefan Stojkovski
  • 479
  • 2
  • 8
  • 17
  • 4
    That's operator overloading abuse 101. Use a member function, please, like `Find_and_remove` or something like that. – jrok Aug 29 '13 at 10:53
  • Its inefficient to delete an element from an array, consider a linked list. – HAL Aug 29 '13 at 10:54
  • If i knew sth. like that i would have! – Stefan Stojkovski Aug 29 '13 at 10:57
  • @HAL: Depends on where you're deleting. – thokra Aug 29 '13 at 10:57
  • 1
    Ignoring the operator abuse, use `std::vector::erase()`. – Mats Petersson Aug 29 '13 at 10:58
  • @jrok Except that if I understand him correctly, the assignment requires using `operator -=`. Not a very good course, IMHO: teaching memory management (a very advanced topic) before `std::vector` (a fairly elementary topic); teaching operator overload abuse; and who knows what else that's bad. – James Kanze Aug 29 '13 at 11:00
  • @thokra offcourse, but prepare for the worst and hope for the best :) – HAL Aug 29 '13 at 11:01
  • @HAL Have you actually measured this. You generally want to avoid linked lists, because they are very, very inefficient. – James Kanze Aug 29 '13 at 11:01
  • @HAL: No, think carefully about your design and use containers that fit. – thokra Aug 29 '13 at 11:04
  • @JamesKanze No but isn't it better than deleting an element(or shifting the elements)in an array? – HAL Aug 29 '13 at 11:05
  • @thokra Yes, and for this case(with whatever little we know from the question), I felt a list fits better than an array. – HAL Aug 29 '13 at 11:09
  • 1
    If order doesn't matter, he can switch with the last element, then pop. – Jarod42 Aug 29 '13 at 11:19

1 Answers1

2

First, you have to find the object you want to remove: std::find_if is good for this, but if it is classwork, they may expect you to write your own implementation of a linear search. Once you've found the entry, you delete the pointer, and either set it to null (and ensure that all other logic works correctly when there are null pointers in the array), or shift all of the following entries down one. (std::copy could be used for the shift.) Of course, if you shift, you'll also have to keep track of where the valid entries in the array end.

There's not really any reason to create a new array when removing objects. Just keep track of the logical end of the array.

James Kanze
  • 150,581
  • 18
  • 184
  • 329