So I've spent some time thinking about this and been all over google looking for a 'solution' (the solution is most likely a matter of preference, but I'm unsure about this). Below is the problem I have, but the problem can be applied to a lot of situations regarding composition.
I have a class, Colour, containing the members of red, green, blue and alpha. Each of these have a set and get member funtion. Simple enough.
class Colour
{
public:
float getRed();
void setRed(float);
float getGreen();
void setGreen(float);
...
private:
float red;
float green;
...
};
I will use this class within other classes to dictate their colour, for example (omitting the constructor and destructor for brevity):
class ColourableObject
{
private:
Colour colour;
};
Now my problem is this: How would this ColourableObject class best access this Colour object? Would it be best to retreive the Colour object to access its member functions directly, like so:
class ColourableObject
{
public:
Colour& getColour();
private:
Colour colour;
};
Or, would it be better to give the ColourableObject class its own set and get functions for colour, in which act upon the Colour object, like so:
class ColourableObject
{
public:
float getRed();
void setRed(float);
...
private:
Colour colour;
};
To me, the former is the most logical as it would save a lot of trouble adding functionality to each class that requires this Colour object by simply acting directly upon this Colour object.
However, isn't the latter more susceptible for changes to the ColourableObject class? Not to mention that colourableObject.getColour().setRed(x) doesn't seem too natural to me, as the get and set clash with each other.
I probably have the wrong approach entirely. I'm relatively new to C++, so I'm willing to learn! So the question is, should I use the former method, latter method, or an entirely different method?