mardi 7 avril 2020

C++, How can I copy builder pattern object parameter to other class's member?

class Email
{
public:
  Email(EmailBuilder builder){
     ...
  }

  public class EmailBuilder {
     EmailBuilder() {...};
     EmailBuilder SetAddress(...) {...};
     EmailBuilder SetSubject(...) {...};
     EmailBuilder SetBody(...) {...};
     Email Build() {...};
  }
};  

class EmailSender
    {
    public:
       ...
       SendEmail(const Email& mail)
       {
           mEmail = mail; //error. I cannot copy parameter 'mail' to 'mEmail'.
       }
    private:
       Email mEmail;
    }


    int main()
    {
        Email mail = new Email.Builder
                            .SetAddress("me@mail.com")
                            .SetSubject("C++ builders")
                            .SetBody("I like this API, don't you?")
                            .Build();
        ...
        EmailSender emailSender;
        ...
        emailSender.SendEmail(mail);
        ...
    }

I want to copy parameter(=mail) to local object(=mEmail).

But I cannot it.

(Maybe it is related to that Email class have no constructor.)

How can I copy paramter(=mail) to EmailSender member object(=mEmail) during use builder pattern?

Aucun commentaire:

Enregistrer un commentaire