returning an abstract class from a function

后端 未结 7 793
甜味超标
甜味超标 2020-12-03 07:07

Is it possible to return an abstract class(class itself or a reference, doesn\'t matter) from a function?

7条回答
  •  既然无缘
    2020-12-03 07:27

    You can declare the return type to be a reference or pointer to the abstract class, so that it can be assigned to references or pointers to the abstract class and used based on its interface.

    However, you cannot return an actual instance of the actual abstract class because by definition you cannot instantiate it. You could, however, return instances of concrete subtypes which is good enough because by the principle of substitution, you should always be able to use a subtype instead of a supertype.

提交回复
热议问题