Overloading a virtual function in a child class

前端 未结 6 1654
一向
一向 2021-01-08 01:04

I am just testing with virtual keyword and inheritance concepts in c++. I have written a small program:

#include
#include

us         


        
6条回答
  •  孤街浪徒
    2021-01-08 01:38

    That can't work because given a cna_MO *, you can see at compile-time that the pointed object does not (necessarily) have the int overload. If it actually pointed to a base-class object, _mo->print(5); would really have nothing to call. Also there can be an infinite number of (not yet implemented) derived classes that don't have to support this call.

    1. Every derived class must have print(int) - declare it in the base class.

    2. Every derived class need not have print(int) - cna_Mo only works with cna_bsc, so the member should be cna_bsc* _mo.

提交回复
热议问题