T
TheFerryman
How bad is it to include a non const accessor to a container of
objects? For instance
template <class WheelType>
class CarBase
{
typedef std::list<WheelType*> WheelList;
private:
//for the sake of this example, assume there can be any number
//of wheels
WheelList m_Wheels;
//other components
public:
const WheelList& GetWheels()const{return m_Wheels;}
WheelList& GetWheels(){return m_Wheels;}
//other car related methods
};
I think this doesn't look good because I may as well make the
WheelList public... but I hate making member variables public unless
the class is a simple data structure (which this isn't, the real-world
examples are complex)
But if I only have the const accessor how should a client gain access
to the wheels so they can change their properties? (If I create an
iterator class to iterate through the objects how is that any
different to allowing public access)
This thing keeps going around and around my head and I think I'm
starting to obsess on the problem so any help will be greatly
appreciated. What do *you* do in these circumstances?
objects? For instance
template <class WheelType>
class CarBase
{
typedef std::list<WheelType*> WheelList;
private:
//for the sake of this example, assume there can be any number
//of wheels
WheelList m_Wheels;
//other components
public:
const WheelList& GetWheels()const{return m_Wheels;}
WheelList& GetWheels(){return m_Wheels;}
//other car related methods
};
I think this doesn't look good because I may as well make the
WheelList public... but I hate making member variables public unless
the class is a simple data structure (which this isn't, the real-world
examples are complex)
But if I only have the const accessor how should a client gain access
to the wheels so they can change their properties? (If I create an
iterator class to iterate through the objects how is that any
different to allowing public access)
This thing keeps going around and around my head and I think I'm
starting to obsess on the problem so any help will be greatly
appreciated. What do *you* do in these circumstances?