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
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"
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;
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 writeA getA()
to return an object, that is, an instance of A. That obviously doesn’t workA 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"
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;