parallel101 / cppguidebook

小彭老师领衔编写,现代C++的中文百科全书
https://142857.red/book/
Other
715 stars 56 forks source link

让虚函数再次伟大一节中存在笔误 #27

Closed Noob-Bob closed 3 months ago

Noob-Bob commented 3 months ago

原文

把 ??? 部分用一个虚函数顶替:

struct Reducer {
    virtual int init() = 0;
    virtual int add(int a, int b) = 0;
};

int reduce(vector<int> v, Reducer *reducer) {
    int res = reducer.init();
    for (int i = 0; i < v.size(); i++) {
        res = reducer.add(res, v[i]);
    }
    return res;
}

笔误

reduce函数的for循环中通过指针reducer调用add应使用->而非., 后续的代码段也应作必要改正

archibate commented 3 months ago

这不赶紧开个pr

无法顺畅的大口呼吸,是活着的最好证明

---原始邮件--- 发件人: "Xuecheng @.> 发送时间: 2024年8月15日(周四) 中午1:11 收件人: @.>; 抄送: @.***>; 主题: [parallel101/cppguidebook] 让虚函数再次伟大一节中存在笔误 (Issue #27)

原文

把 ??? 部分用一个虚函数顶替: struct Reducer { virtual int init() = 0; virtual int add(int a, int b) = 0; }; int reduce(vector<int> v, Reducer *reducer) { int res = reducer.init(); for (int i = 0; i < v.size(); i++) { res = reducer.add(res, v[i]); } return res; }

笔误

reduce函数的for循环中通过指针reducer调用add应使用->而非., 后续的代码段也应作必要改正

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you are subscribed to this thread.Message ID: @.***>