【发布时间】:2020-05-08 13:02:03
【问题描述】:
我最近在学习设计模式,我看过关于构建器模式的文章,https://riptutorial.com/cplusplus/example/30166/builder-pattern-with-fluent-api。我对对象的实例化有疑问。下面是文章里面的代码:
#include <iostream>
#include <sstream>
#include <string>
using namespace std;
// Forward declaring the builder
class EmailBuilder;
class Email
{
public:
friend class EmailBuilder; // the builder can access Email's privates
static EmailBuilder make();
string to_string() const {
stringstream stream;
stream << "from: " << m_from
<< "\nto: " << m_to
<< "\nsubject: " << m_subject
<< "\nbody: " << m_body;
return stream.str();
}
private:
Email() = default; // restrict construction to builder
string m_from;
string m_to;
string m_subject;
string m_body;
};
class EmailBuilder
{
public:
EmailBuilder& from(const string &from) {
m_email.m_from = from;
return *this;
}
EmailBuilder& to(const string &to) {
m_email.m_to = to;
return *this;
}
EmailBuilder& subject(const string &subject) {
m_email.m_subject = subject;
return *this;
}
EmailBuilder& body(const string &body) {
m_email.m_body = body;
return *this;
}
operator Email&&() {
return std::move(m_email); // notice the move
}
private:
Email m_email;
};
EmailBuilder Email::make()
{
return EmailBuilder();
}
// Bonus example!
std::ostream& operator <<(std::ostream& stream, const Email& email)
{
stream << email.to_string();
return stream;
}
int main()
{
Email mail = Email::make().from("me@mail.com")
.to("you@mail.com")
.subject("C++ builders")
.body("I like this API, don't you?");
cout << mail << endl;
}
谁能解释一下Email mail = Email::make().from("me@mail.com"),它是如何工作的?
【问题讨论】:
-
"有人能解释一下
Email mail = Email::make().from("me@mail.com"),它是如何工作的吗?" 关于它,你不清楚什么?
标签: c++ oop design-patterns