审查生成的报表接口

来自 虚函数
C++23 高级 6分钟 找出 4处问题

根据任务契约审查这个生成的类层次。

通过 Report 返回拥有型报表,以多态方式渲染并安全销毁,同时在检查文本时不产生副本。

C++
#include <memory>
#include <string>
#include <utility>

class Report {
public:
    explicit Report(std::string text) : text_(std::move(text)) {}
    virtual std::string render() const { return text_; }
    std::string text() const { return text_; }
    ~Report() = default;
protected:
    std::string text_;
};

class HtmlReport final : public Report {
public:
    using Report::Report;
    std::string render() { return "<p>" + text_ + "</p>"; }
};

std::unique_ptr<Report> make_report() {
    return std::make_unique<HtmlReport>("ready");
}

生成代码仅作示例,不代表任何特定模型

在试验场中打开
报告错误