Эксперт
- Статус
- Оффлайн
- Регистрация
- 12 Июн 2014
- Сообщения
- 994
- Реакции
- 1,209
Зачем: попросили
Для чего: хз(инфы в интернете море)
Просто пример базового виртуального класса и работа с этим.
Наследники описателя:
Просто перегружаем своими методами.
Ex:
Для чего: хз(инфы в интернете море)
Просто пример базового виртуального класса и работа с этим.
Код:
class BaseVirtual
{
public:
~BaseVirtual(void) = default;
virtual void drawLine(const float& a1, const float& b1, const float& a2, const float& b2) = 0;
virtual void drawRect(const float& x, const float& y, const float& w, const float& h) = 0;
virtual void drawBord(const float& x, const float& y, const float& w, const float& h) = 0;
protected:
BaseVirtual& operator=(const BaseVirtual&) = delete;
};
Наследники описателя:
Код:
class RenderDx9:virtual public BaseVirtual
{
public:
virtual void drawLine(const float& a1, const float& b1, const float& a2, const float& b2) override{
printf_s(" Dx9 Line: (%.1f, %.1f) (%.1f, %.1f)\n",a1,b1,a2,b2);
}
virtual void drawRect(const float& x, const float& y, const float& w, const float& h) override{
printf_s(" Dx9 Rect: (%.1f, %.1f, %.1f, %.1f)\n", x, y, w, h);
}
virtual void drawBord(const float& x, const float& y, const float& w, const float& h) override{
printf_s(" Dx9 Bord: (%.1f, %.1f, %.1f, %.1f)\n", x, y, w, h);
}
RenderDx9(int a){
printf_s(" RenderDx9 %d\n",a);
}
~RenderDx9(void) = default;
};
class RenderDx11 :virtual public BaseVirtual
{
public:
virtual void drawLine(const float& a1, const float& b1, const float& a2, const float& b2) override {
printf_s(" Dx11 Line: (%.1f, %.1f) (%.1f, %.1f)\n", a1, b1, a2, b2);
}
virtual void drawRect(const float& x, const float& y, const float& w, const float& h) override {
printf_s(" Dx11 Rect: (%.1f, %.1f, %.1f, %.1f)\n", x, y, w, h);
}
virtual void drawBord(const float& x, const float& y, const float& w, const float& h) override {
printf_s(" Dx11 Bord: (%.1f, %.1f, %.1f, %.1f)\n", x, y, w, h);
}
RenderDx11(void) {
printf_s(" RenderDx11" );
}
~RenderDx11(void) = default;
};
Ex:
Код:
void CallTest(BaseVirtual* arg)
{
arg->drawLine(10.f, 11.f, 123.f, 444.f);
arg->drawRect(1.f, 1.f, 100.f, 100.f);
arg->drawBord(2.f, 2.f, 200.f, 100.f);
printf_s("\n");
}
Код:
printf_s("Call dx9:\n");
auto renderer_dx9 = new RenderDx9(10);
CallTest(renderer_dx9);
delete renderer_dx9;
printf_s("Call dx11:\n");
auto renderer_dx11 = new RenderDx11;
CallTest(renderer_dx11);
delete renderer_dx11;
printf_s("Call dx11(make_shared):\n");
auto shared_dx11 = std::make_shared<RenderDx11>();
CallTest(shared_dx11.get());