c++ - c++中的作用域和继承
问题描述
我想知道为什么编译器不允许第二次使用“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 *>
is an unrelated type to list<bar *>
. The function is specified to accept one, but not the other.
But class bar inherits from class foo
That is irrelevant, because the argument of your function isn't foo&
. What's relevant is whether list<bar *>
inherits list<foo *>
. It doesn't. std::list
does not have a base class.
推荐阅读
- c++ - 在这种情况下,我真的是在复制字节还是在复制字符?
- docker - docker私有注册表是否有默认端口
- oracle - oracle单行变成变量
- python - 模型类默认关键字如何在 sqlalchemy 中工作
- sql - “插入选择”如何处理大量数据
- javascript - 如何确定是否点击了地图或标记
- facebook - 如何量化 Facebook 页面活动?(图形 API)
- php - 在注册表单中添加 WordPress(WooCommerce) 中的 OTP 验证
- excel - 查找 Excel 中存在值的列的最后一行索引
- angular - 如何替换ngFor中的null值