What is the best practice to implement following scenario:
Class A
holds a member object of Type B
.
class A
{
private:
B b;
};
class B
{
private:
int x;
};
A print function which gets an object of type A
as const &
parameter, should print the members of a
and b
:
void Print(const A& a)
{
cout << a.b.x;
}
A read function should set the values of a
(and b
) and their members:
void Read(A& a)
{
// ...
a.b.x = 2;
}
How should class A
be implement regarding its member access?
- Should "
b
" be public? - Should "
class A
" provide 2 getters for b (1 for write and 1 read access to "b")?
Additional information:
In my real system the classes A
and B
are much larger and they are part of a huge legacy system. In this legacy system Print
and Read
are member functions of a "View-Model", where Print
writes the values to the GUI and Read
reads the values from the GUI and sets the members of A
and B
. So the resposibility of A
and B
is to hold the data (some kind of data models).