Home  >  Q&A  >  body text

c++ - 返回值如果想要传值而不是传引用或指针是不是无法做到多态?

阿神阿神2764 days ago576

reply all(3)I'll reply

  • ringa_lee

    ringa_lee2017-04-17 13:34:37

    This seems to have little to do with polymorphism, mainly because you want it 实例化一个纯虚类,这是非法的.
    You have a A::foo()=0 indicating that A is a pure virtual class, but you also write A getA() to return an object, that is, an instance of A. That obviously doesn’t work

    reply
    0
  • PHP中文网

    PHP中文网2017-04-17 13:34:37

    A is an abstract class and is not allowed to be instantiated. In other words, class A will not have objects. The getA() method in class B returns an object of A, which violates the principle of "not allowed to be instantiated"

    reply
    0
  • 迷茫

    迷茫2017-04-17 13:34:37

    1 Abstract classes cannot be instantiated, so the compiler reports an error;
    2 If you use objects instead of references or pointers, polymorphic effects cannot be achieved;

    reply
    0
  • Cancelreply