[操作疑難] C++ Virtual 問題

Rectangle 有無可能唔 Override draw 嘅情況下,
可以 Call 得到 Screen::drawShape(Rectangle*) ?

class Shape {
    public:
        virtual void draw ( Screen* scr ) {
              scr -> drawShape ( this );
       }
}

class Rectangle:public Shape { }

class Screen {
    public:
       void drawShape ( Shape *s ) {
            cout << "Draw Shape" ;
       }

       void drawShape ( Rectangle *r ) {
            cout  << "Draw Rectangle" ;
       }
}

做左type casting先?

TOP

既然佢係 virtual,
expect 唔只 1 個 inherit 啦。

做左type casting先?
KinChungE 發表於 2021-3-28 14:32

TOP

Let's put aside C++ first. The design is really hard to understand. Is it really needed to do it that way?

TOP

我自己認為呢個 design 係一個幾 common 嘅諗法

Shape:
   Rectangle
   Triangle
   Circle ...

Driver:
   GenericDriver
      CustomDriverA
      CustomDriverB

將來如果有人想整個 CustomDriverC
嘅話,佢可以唔洗改 Parents 嘅情況下,
自己 Overload 一個 implement。

TOP

我寫緊嘅 Library 其實就係爭呢樣嘢

https://github.com/ghostkcleung/tft_framework

TOP

我反如唔係好明, 如果個drawShape係專俾Rectangle用, 點解唔放去Rectangle class override draw(), 而要放響Screen度overload?

TOP

我反如唔係好明, 如果個drawShape係專俾Rectangle用, 點解唔放去Rectangle class override draw(), 而要放 ...
KinChungE 發表於 2021-3-29 10:36


因為其實唔只 Rectangle 一個 Shape

TOP

因為其實唔只 Rectangle 一個 Shape
ghostkcleung 發表於 2021-3-29 16:46


咁咪每個shape既child class各自override
OO原意就係咁
響parent做哂child要做既野有D本末倒置

TOP

咁咪每個shape既child class各自override
OO原意就係咁
響parent做哂child要做既野有D本末倒置 ...
KinChungE 發表於 2021-3-29 16:59


依家唯有係咁。

變咗每個 Child override 都係寫重複嘅嘢。

TOP