我想知道为什么编译器不允许第二次使用"print_all"函数。
我必须举一个例子,如果编译器允许,可能会发生一件坏事
#include <iostream>
#include <list>
using std::list;
class foo {
class bar : public foo {
static void print_all(list<foo *> &L) {
list<foo *> LF;
list<bar *> LB;
print_all(LF); // works fine
print_all(LB); // static semantic error
}
};
};
list<foo *>
是与list<bar *>
不相关的类型。函数被指定为接受一个,但不接受另一个。
但是类bar继承自类foo
这无关紧要,因为函数的参数不是foo&
。相关的是list<bar *>
是否继承了list<foo *>
。事实并非如此。std::list
没有基类。