Here's my problem.
I have a class that holds an array of objects (in a std::vector) These object can be modified from client code, so I made a private getter that returns a pointer to the object I need to modify. The public methods use this getter to modify the objects in the array.
The private getter is also used in other member functions that return some specific values of the objects in the array to client code. I would like these functions to be and return const, but I can not do that because I use the non-const getter earlier mentioned.
I know I could make one other const getter, but that would just duplicate code.
How can implement this correctly?
Code example:
class Object;
class Inventory
{
Object* GetObject(int id);
void AddObjectProp(int id, int amount) {
Object* x = GetObject id);
x->prop += amount;
}
//using const here is not posible because GetObject is not const
int GetObjectProp(int id) {
Object* x = GetObject id);
return x->prop;
}
}
Thank you.