Deriving an abstract class from concrete class

后端 未结 7 820
囚心锁ツ
囚心锁ツ 2021-02-05 16:05

Let\'s say we have a concrete class Apple. (Apple objects can be instantiated.) Now, someone comes and derives an abstract class Peach from Apple. It\'

7条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-05 16:33

    To answer your first question, you can do this since users of Apple, if given a concrete instance derived from Peach will not know any different. And the instance will not know its not an Apple (unless there are some virtual functions from Apple that are overridden that you didn't tell us about).

    I can't yet imagine how useful it would be to override a virtual function with a pure virtual one - is that even legal?

    In general you want to conform with Scott Meyers "Make all non-leaf classes abstract" item from his books.

    Anyway, apart from that what you describe seems to be legal - its just that I can't see you needing it that often.

提交回复
热议问题