[英]include classes in header files in other header files
I have two classes Screen and Window_Mgr(C++ Primer 5th Edition Chapter 7). 我有两个类Screen和Window_Mgr(C ++ Primer第5版第7章)。
I want to write this code in such a way that only Window_Mgr::clear is a friend of Screen. 我想以这样的方式编写这段代码,只有Window_Mgr :: clear才是Screen的朋友。
The problems I am faced with are: 我面临的问题是:
I tried including them with separate header files but that was a real mess. 我尝试用单独的头文件包含它们,但这真是一团糟。
#include <iostream>
#include <vector>
class Screen;
struct Window_Mgr{
public:
// using Screen_Index = std::vector<Screen>::size_type;
typedef std::vector<Screen>::size_type Screen_Index;
void clear(Screen_Index i);
Window_Mgr() = default;
private:
std::vector<Screen> Screens;
};
struct Screen{
typedef std::string::size_type pos;
friend void Window_Mgr::clear(Screen_Index i);
Screen() = default;
Screen(pos h, pos w, char s): height(h), width(w), contents(h*w,s){};
char get() const { return contents[cursor];}
char get(pos ht, pos width)const;
Screen &move(pos r, pos c);
Screen &set(char);
Screen &set(pos, pos, char);
Screen & display(std::ostream &);
const Screen & display (std::ostream &) const;
pos size() const;
private:
pos cursor = 0;
pos height = 0, width = 0;
std::string contents;
const Screen do_display (std::ostream& os) const
{
os << contents;
return *this;
}
};
consider using classes instead and your forward declaration is for a class, where you create screen as a struct. 考虑使用类而不是你的前向声明用于一个类,你可以在其中创建一个结构的屏幕。 This might be causing some of your issues.
这可能会导致您的一些问题。 and instead of using a friend how about two separate clear functions one for Window_mgr that calls a different clear function in Screen.
而不是使用朋友如何为Window_mgr调用两个单独的清除函数,在Screen中调用不同的清除函数。
your window_mgr clear would be something like 你的window_mgr清楚会是这样的
void Window_Mgr::clear(int i)
{
Screens.at(i).clear();
}
and your Screen clear would be something like 你的屏幕清晰就像是
void Screen::clear()
{
//whatever you want to do to private variables here
}
And as one of my professors beat into our heads "Friends of classes are not friends of programmers" 正如我的一位教授击败了我们的脑袋“课程之友不是程序员的朋友”
You are violating encapsulation by having Screen
call Window_Mgr
. 您通过
Screen
调用Window_Mgr
来违反封装。
The Window_Mgr
is a container of Screen
s. Window_Mgr
是Screen
的容器。 The Window_Mgr
should call the clear
method of a screen. Window_Mgr
应该调用屏幕的clear
方法。
struct Screen
{
void clear()
{
//...
}
};
struct Window_Mgr
{
std::vector<Screen> Screen_Container;
void clear_screen(unsigned int screen_index)
{
Screen_Container[screen_index].clear();
}
};
Due to simplicity, I have not placed in checks for index ranges. 由于简单,我没有检查索引范围。
Edit 1: Friendship. 编辑1:友谊。
There is no need for friendship here. 这里不需要友谊。 The
Window_Mgr
can only access what the Screen
class has defined in its interface. Window_Mgr
只能访问Screen
类在其界面中定义的内容。
Also, the Screen
class should have no knowledge of whether it is in a container or not; 此外,
Screen
类应该不知道它是否在容器中; that concept belongs to the Window_Mgr
class. 该概念属于
Window_Mgr
类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.