Given the following code:
class Screen;
class WindowMgr
{
WindowMgr& relocateScreen( int r, int c, Screen& s);
};
class Screen
{
friend WindowMgr& WindowMgr::relocateScreen( int r, int c, Screen& s);
// ^ cannot access private member declared in class 'WindowMgr'
int m_nR,
m_nC;
};
WindowMgr& WindowMgr::relocateScreen( int r, int c, Screen& s)
{
s.m_nR = r;
s.m_nC = c;
return *this;
}
Why can the Screen
class not declare the WindowMgr::relocateScreen
member function as a friend? Screen
is not wanting to use this private member-function of another class, but simply wants that function to be able access its own private members.
Making the relocateScreen
function public might be bad design if it's intented only for use within the WindowMgr
class. Equally, making Screen
a friend of WindowMgr
might be bad design if it is not intented to access private members of WindowMgr
in any other case.
Where am I going wrong here? What is the correct approach? Am I making a fool of myself?