简体   繁体   English

包括其他头文件中的头文件中的类

[英]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章)。

  1. Screen is a class that contains three string::size_types and a string as data members. Screen是一个包含三个string :: size_types和一个字符串作为数据成员的类。
  2. Window_Mgr is a class that contains an object Screens of type vector< Screen > . Window_Mgr是包含式矢量< 屏幕 >的目的屏幕的类。
  3. It initializes the vector< Screen > with the values (24,80,' ') with a constructor in Screen. 它使用Screen中的构造函数初始化带有值(24,80,'')向量< Screen >
  4. void Window_Mgr::clear(vector::size_type i) is a function that accesses the private members of string and clears them of all data. void Window_Mgr :: clear(vector :: size_type i)是一个访问字符串私有成员并清除所有数据的函数。

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: 我面临的问题是:

  1. If I define Window_Mgr before Screen with an incomplete declaration I can't initialize Screens . 如果我在Screen之前使用不完整的声明定义Window_Mgr,则无法初始化Screens
  2. If I define Screen before Window_Mgr with an incomplete declaration I can't declare Window_Mgr::clear as a friend of Screen . 如果我在Window_Mgr之前使用不完整的声明定义屏幕,我不能将Window_Mgr :: clear声明为Screen的朋友。

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_MgrScreen的容器。 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM