Возможные реализации для решения конкретных задач на С++
Прототип. Идиома NVI (Non-Virtual Interface)
class Car
{
public:
virtual ~Car() = default;
unique_ptr<Car> clone();
private:
virtual unique_ptr<Car> doClone() = 0;
};class Sedan : public Car
{
public:
Sedan()
{
cout << "Calling the default constructor;" << endl;
}
Sedan(const Sedan& car)
{
cout << "Calling the Copy constructor;" << endl;
}
~Sedan() override
{
cout << "Calling the destructor;" << endl;
}
private:
unique_ptr<Car> doClone() override
{
return make_unique<Sedan>(*this);
}
};# include <iostream>
# include <memory>
# include <exception>
using namespace std;
int main()
{
try
{
shared_ptr<Car> luxsedan = make_shared<LuxurySedan>();
User{}.use(luxsedan);
}
catch (exception& err)
{
cout << err.what() << endl;
}
}Прототип (Prototype). «Статический полиморфизм» (CRTP)
Прототип (Prototype). «Статический полиморфизм» (CRTP) и множественное наследование
Прототип (Prototype). Ковариантный виртуальный метод. Идиомы CRTP и NVI
Last updated