I have two classes which resemble the following.
class Hand
{
public:
func1();
func2();
...
func20();
private:
Hand();
~Hand();
Hand(const Hand& that);
void operator=(Hand const&);
friend class Body;
};
class Body
{
public:
Body();
~Body();
Hand * const &left_hand;
Hand * const &right_hand;
private:
Hand * _left_hand;
Hand * _right_hand;
};
Body::Body()
:left_hand(_left_hand)
:right_hand(_right_hand)
{
_left_hand = new Hand();
_right_hand = new Hand();
};
This follows the Hand/Body relation. The hand belongs to the Body. -
- User cannot create/delete a Hand object.
- User cannot create a copy of the Hand object belonging to the Body.
- User cannot modify the left hand and right hand pointers of the Body object.
However, my colleagues say that to allow the user to directly call methods of a member object (in this case body->left_hand->func1()) is a bad design.
One suggestion was to use a getter funtion eg getLeftHand() which returns Hand * const instead of a read only public variable left_hand.
Another was to create wrapper functions for each Hand functions like
class Body
{
Body();
~Body();
leftHandfunc1();
rightHandfunc1();
leftHandfunc2();
rightHandfunc2();
...
leftHandfunc20();
rightHandfunc20();
private:
Hand * _left_hand;
Hand * _right_hand;
};
And
Body::leftHandfunc1()
{
_left_hand->func1();
}
However, as you can see, 20 methods of Hand equals 40 wrapper functions in Body. And this list is expected to grow. Should I go with this approach?
Is there any better alternative?
Handby introducing an abstract base class (the interface)?class Bodyhas onlyprivatememebrs and nopublicis that a typo?Hand¬Hand*.body->left_hand->func1()in the application would be OK?