Best way to amend/delete items stored in an object?

Hi

I have a class Container like this:

1
2
3
4
5
6
7
8
9
10
11
class Container{

private:

vector<CustomType> things;

public:

void addThing(CustomType);
vector<CustomType> getThings();
}


I understand it is better to provide the client with a copy rather than return a reference to 'things' (or any element thereof).

However, it seems to me that if the client wants to amend a thing then the only way of doing it is by asking the user to provide a copy of the thing and then a new thing which will replace it. As follows

 
void amendThing(CustomType old_thing, CustomType new_thing)


Similarly, deleting a thing would just be searching for any thing and deleting the first identical copy.

Is this the best way of doing it or am I missing something?

Thanks
Last edited on
A user will ask:
What do I gain/lose by using a Container and not the vector<CustomType> directly?
Container has all sorts of other members - I was just abstracting for the purposes of the question.
If "amend a thing" means updating or changing the proper of CustomType, then the user does not have to provide the old_thing. The user just provides a new_thing and you do as follows

1
2
3
4
5
For thing in things,
  if(new_thing is present)
      change_property
   else
     tell_user_that_new_thing_is_not_present

making copies of fat things when it isnt necessary is a performance hit.
This is why you can promote things to be constant if you don't want them to change it, can you not provide a const ref instead of a copy?
I have a class Container like this:

Since this is a "container" the "container" doesn't need to know anything about the class being "contained".

The container should probably have methods to insert, remove, replace an element in the container, look at the standard containers for examples of what may be needed.



Topic archived. No new replies allowed.