考虑一下代码:
#include <stdio.h>
class Base {
public:
virtual void gogo(int a){
printf(" Base :: gogo (int) \n");
};
virtual void gogo(int* a){
printf(" Base :: gogo (int*) \n");
};
};
class Derived : public Base{
public:
virtual void gogo(int* a){
printf(" Derived :: gogo (int*) \n");
};
};
int main(){
Derived obj;
obj.gogo(7);
}
Run Code Online (Sandbox Code Playgroud)
得到此错误:
>g++ -pedantic -Os test.cpp -o test test.cpp: In function `int main()': test.cpp:31: error: no matching function for call to `Derived::gogo(int)' test.cpp:21: note: candidates are: virtual …
(事先原谅noob问题)
我有4个班:
class Person {};
class Student : public Person {};
class Employee : public Person {};
class StudentEmployee : public Student, public Employee {};
Run Code Online (Sandbox Code Playgroud)
基本上Person是基类,其直接由两个子类Student和Employee.StudentEmployee使用多重继承来子类化Student和Employee.
Person pat = Person("Pat");
Student sam = Student("Sam");
Employee em = Employee("Emily");
StudentEmployee sen = StudentEmployee("Sienna");
Person ppl[3] = {pat, sam, em};
//compile time error: ambiguous base class
//Person ppl[4] = {pat, sam, em, sen};
Run Code Online (Sandbox Code Playgroud)
当我使用Person基类的数组时,我可以将Person它的所有子类放在这个数组中.除了StudentEmployee,给出基础模糊的原因. …