我是 C++ 的新手,目前正在阅读 C++ Primer,在第 7 章(讨论最基本的类特性)中,它谈到了当我们想让成员函数成为另一个类的友元时“仔细结构化”的重要性:
*“使成员函数成为友元需要仔细构建我们的程序,以适应声明和定义之间的相互依赖性。在这个例子中,我们必须按如下顺序排列我们的程序:
我的问题是:
非常感谢!
以下是书中 Window_mgr 和 Screen 的示例代码,用于上下文:
class Screen {
friend void Window_mgr::clear(ScreenIndex);
//...rest of the Screen class
};
class Window_mgr {
public:
using ScreenIndex = std::vector<Screen>::size_type;
void clear(ScreenIndex);
// rest of the class
};
void Window_mgr::clear(ScreenIndex i)
{
// s is a reference to the Screen we want to clear
Screen& s = screens[i];
// reset the contents of that Screen to all blanks
s.contents = string(s.height * s. width, ' ');
}
这篇文章认为一般不了解朋友/班级......所以任何细节都可以提供帮助。谢谢!