Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

c++ - Member function a friend

I've been trying some examples in a book (C++ Primer by Stanley Lippman) and I understand that a class can make another class its friend (to access some private members). Now I'm reading about a member function being a friend and I try the example

class Screen
{
public:
    friend void Window_mgr::clear();

    typedef std::string::size_type pos;

    Screen () = default;
    Screen (pos ht, pos wd, char c) : height (ht), width (wd),
                                      contents (ht * wd, c) { }

private:
    void do_display (std::ostream &os) const
    {
        os << contents;
    }

    pos cursor = 0;
    pos height = 0, width = 0;
    pos test_num = 100, test_num2 = 222;;
    std::string contents = "contents";
   };

  class Window_mgr {
 public:
     using ScreenIndex = std::vector<Screen>::size_type;
     void clear (ScreenIndex);

 private:
     std::vector <Screen> screens {Screen (24, 80, ' ')};
 };

 void Window_mgr::clear(ScreenIndex i)
{
Screen &s = screens[i];
s.contents = std::string(s.height * s.width, ' ');
}

but it produces a compiler error saying

Window_mgr has not been declared

and then I read this:

? First, de?ne the Window_mgr class, which declares, but cannot de?ne, clear. Screen must be declared before clear can use the members of Screen.

? Next, de?ne class Screen, including a friend declaration for clear.

? Finally, de?ne clear, which can now refer to the members in Screen.

I don't understand this part -- can someone explain?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You are most of the way there, you just need to ensure that you declare all of your classes and functions before you require that declaration.

The first instruction point says to define the Window_mgr class declaring Window_mgr::clear. In order to use Screen, that class must also be declared before Window_mgr. This looks like:

class Screen; //forward-declare Screen so that Window_mgr knows it exists

class Window_mgr {
public:
    //requires forward declaration of Screen, like the above
    using ScreenIndex = std::vector<Screen>::size_type;
    void clear (ScreenIndex); //declare, but don't define, clear
};

The second point says to define Screen and include a friend declaration for Window_mgr::clear. Because that member function has already been declared above, this is valid:

class Screen
{
public:
    using ScreenIndex = std::vector<Screen>::size_type;
    //remember to friend with the same arguments
    friend void Window_mgr::clear(ScreenIndex);
    //...
};

The final point tells you to now define Window_mgr::clear. We have already declared in in the first point, so we just need to do this:

void Window_mgr::clear(ScreenIndex i)
{
    //...
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...