c++ - 为什么继承在方法内部不起作用?
问题描述
这不会编译:
struct Base
{
void something( int a ) { }
};
struct Derived : public Base
{
static void something()
{
std::unique_ptr<Derived> pointer = std::make_unique<Derived>();
pointer->something( 11 );
}
};
可以修复using Base::something
但仍然可以使继承工作,即使在方法内部也能像宣传的那样工作?
解决方案
通过在派生类中为函数使用相同的名称,您可以从基类中隐藏符号。
using
您可以通过使用以下语句从基类中提取名称来解决它:
struct Derived : public Base
{
// Also use the symbol something from the Base class
using Base::something;
static void something()
{
std::unique_ptr<Derived> pointer = std::make_unique<Derived>();
pointer->something( 11 );
}
};
推荐阅读
- excel - excel中忽略NA值的平均值
- html - 从 Node REST API 返回图像并在客户端的 img.src 中使用
- ruta - 是否需要 RUTA 脚本中的一些额外设置来检测具有相同开始和结束属性的注释?
- firebase - 使用 Firebase 规则在 Firestore 中进行数据验证
- c# - 通过 DI 解决身份验证事件处理程序的依赖关系
- xml - XSLT 外部参数拆分
- wpf - DatagridComboBoxColumn 排序
- xml - 在 Spring XML 中声明一个 bean 为主
- machine-learning - 数据挖掘中的异常值处理
- node.js - 5000 端口一直在使用