我想知道为什么编译器不允许第二次使用“ 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
}
};
};
答案 0 :(得分:1)
list<foo *>
与list<bar *>
无关。该函数被指定为接受一个,但是不接受另一个。
但是class bar继承自foo类
这无关紧要,因为函数的参数不是foo&
。相关的是list<bar *>
是否继承了list<foo *>
。没有。 std::list
没有基类。