0

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

Javi
  • 3,440
  • 5
  • 29
  • 43
  • 4
    This is a conversion operator, not `operator()` (which is a function call operator). – user975989 Feb 05 '21 at 11:07
  • Ah, I understand now. I was misinterpreting the code. I will mark the question as duplicate then. Thank you for your help! – Javi Feb 05 '21 at 11:11

0 Answers0