I have this piece of code, which implements a Builder pattern (modified from here):
#include <iostream>
using namespace std;
class PersonBuilder;
class Person {
public:
Person(std::string name) : _name(name) {}
std::string _name, _address;
};
class PersonBuilder {
Person person;
public:
PersonBuilder(string name) : person(name) {}
static PersonBuilder create(std::string name) { return PersonBuilder{name}; }
operator Person() const { return move(person); } // When is this called?
PersonBuilder& lives_at(std::string address) {
person._address = address;
return *this;
}
};
int main() {
Person p = PersonBuilder::create("John").lives_at("World");
cout << p._name << " " << p._address << endl;
return 0;
}
The code works as expected, its output is: John World
.
However, I am failing to understand where/when the Person()
operator is called
Any help to understand this is appreciated