#include <memory>
class Base
{
std::shared_ptr<Base> create() const; // Returns a default constructed object
}
Suppose, that all the members derived to whatever degree from Base are copy constructible and default constructible. I want the
std::shared_ptr<Base> create() const;
Method to create object of the appropriate dynamic type, but I do not want to use boilerplate code.
Is it possible to make
std::shared_ptr<Base> create() const;
Statically bound, but inside find somehow the correct type and create the object using Default constructor? Possibly using C++11.
The
create()functions should probably be static, as you don’t have an instance yet. But without parameters you cannot do what you want… unless you use templates, of course:Then use it this way:
Or, if you prefer: