C++ Primer 练习7.32(C++ Primer读书笔记)

第七章 类

练习7.32  定义你自己的Screen和Window_mgr,其中clear是Window_mgr的成员,是Screen的友元。

由于Window_mgr中含有Screen对象,所以在Window_mgr之前要声明Screen。

因为clear中调用Screen成员,所以Screen要在clear定义之前定义。

因为Screen类中要声明友元函数clear,而clear是Window_mgr的成员函数,所以在Screen之前要定义Window_mgr。

好乱啊啊啊啊

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <iostream>
#include <string>
#include <vector>
 
class Screen;
 
class Window_mgr {
public:
    using ScreenIndex = std::vector<Screen>::size_type;
    void clear(ScreenIndex);
private:
    std::vector<Screen> screens;//{Screen(24, 80, ' ')};
};
 
class Screen {
public:
    typedef std::string::size_type pos;
    Screen() = default;
    Screen(int ht, int wd) : height(ht), width(wd), contents(ht * wd, ' ') {}
    Screen(pos ht, pos wd, char c) : height(ht), width(wd), contents(ht * wd, c) {}
    char get() const    // 读取光标处的字符
    {
        return contents[cursor];
    }
    inline char get(pos ht, pos wd) const;
    Screen &move(pos r, pos c);
    void some_number() const;
    Screen &set(char);
    Screen &set(pos, pos, char);
    Screen &display(std::ostream &os)
    {
        do_display(os);
        return *this;
    }
    const Screen &display(std::ostream &os) const
    {
        do_display(os);
        return *this;
    }
    //friend class Window_mgr;
    friend void Window_mgr::clear(ScreenIndex);
private:
    pos cursor = 0;
    pos height = 0, width = 0;
    mutable size_t access_ctr;
    std::string contents;
    void do_display(std::ostream &os) const
    {
        os << contents;
    }
};
 
inline Screen &Screen::move(pos r, pos c)
{
    pos row = r * width;
    cursor = row + c;
    return *this;
}
 
char Screen::get(pos r, pos c) const
{
    pos row = r * width;
    return contents[row + c];
}
 
void Screen::some_number() const
{
    ++access_ctr;
}
 
inline Screen &Screen::set(char c)
{
    contents[cursor] = c;
    return *this;
}
 
inline Screen& Screen::set(pos r, pos col, char ch)
{
    contents[r * width + col] = ch;
    return *this;
}
 
void Window_mgr::clear(ScreenIndex i)
{
    Screen &s = screens[i];
    s.contents = std::string(s.height * s.width, ' ');
}
 
int main()
{
    Screen myscreen(5, 5, 'X');
    myscreen.move(4, 0).set('#').display(std::cout);
    std::cout << '\n';
    myscreen.display(std::cout);
    return 0;
}

  

对于这一句

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

暂时只能改成

1
std::vector<Screen> screens;

因为第一个需要调用构造函数,那样的话。。。Screen就要定义在Window_mgr前面。。。。我要疯了。。。。

posted @   我不吃饼干呀  阅读(944)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
点击右上角即可分享
微信分享提示