我只是用c中的虚拟关键字和继承概念进行测试.
我写了一个小程序:
我写了一个小程序:
#include<stdio.h> #include<iostream> using namespace std; class cna_MO { public: virtual void print() { cout << "cna_MO" << endl; } }; class cna_bsc:public cna_MO { public: void print() { cna_MO::print(); } void print(int a) { cout << "cna_BSC" << endl; } }; class cna_Mo { cna_MO *_mo; public: cna_Mo() { _mo = new cna_bsc; } virtual void print(int a) { cout << "cna_Mo with arg" << endl; _mo->print(5); } virtual void print() { cout << "cna_Mo" << endl; _mo->print(); } }; int main() { cna_Mo valid_mo; cout << "old case is started" << endl; valid_mo.print(); cout << "new case is started" << endl; valid_mo.print(5); return 0; }
我在这里做的是我已经重载了一个虚拟函数在父类中的子类!这不是正确的事吗?
我收到的编译错误如下:
“temp10.cc”,line 45: Error: Too many arguments in call to “cna_MO::print()”.