I am trying to create a constructor with similar arguments for some project classes.
The way I do this is to define the proxy class of the class that will contain the arguments and pass an instance of this proxy file to the constructor of my classes.
Everything worked fine until I had to output one of my classes.
Basically, I thought: I'm going to get a new proxy of a derived class from a proxy of a base class. This also works, but only if I use only derived proxy class arguments.
Here is an example because it is easier to understand:
class Person
{
public:
class PersonArgs
{
public:
const std::string& Name() const { return _name; }
PersonArgs& Name(const std::string& name)
{
_name = name;
return *this;
}
const std::string& Surname() const { return _surname; }
PersonArgs& Surname(const std::string& surname)
{
_surname = surname;
return *this;
}
protected:
std::string _name;
std::string _surname;
}
public:
Person()
: _name("")
, _surname("")
{ }
Person(const PersonArgs& args)
: _name(args.Name())
, _surname(args.Surname())
{ }
protected:
std::string _name;
std::string _surname;
}
class PersonEx : public Person
{
public:
class PersonExArgs : public Person::PersonArgs
{
public:
const std::string& Address() const { return _address; }
PersonExArgs& Address(const std::string& address)
{
_address = address;
return *this;
}
protected:
std::string _address;
}
public:
PersonEx()
: _address("")
{ }
PersonEx(const PersonExArgs& args)
: Person(args)
, _address(args.Address())
{ }
protected:
std::string _address;
}
int main(int argc, char** argv)
{
PersonEx* p1 = new PersonEx(PersonEx::PersonExArgs().Address("example"));
PersonEx* p2 = new PersonEx(PersonEx::PersonExArgs().Address("example").Name("Mark"));
}
, , , , , , , .
- , ?