根据任务契约审查这个生成的类层次。
通过 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");
}
生成代码仅作示例,不代表任何特定模型