c++ - "using"关键字是否可以继承较少的功能?

标签 c++ inheritance using

Derived 中有一个 template foo(T)Base 中有 2 个 foo() 重载。

struct Base
{
  void foo (int x) {}
  void foo (double x) {}
};

struct Derived : Base
{
  template<typename T> void foo (T x) {}
  using Base::foo;
};

现在,当 foo()Derived 对象调用时;如果适用,我只想使用 Base::foo(int),否则它应该调用 Derived::foo(T)

Derived obj;
obj.foo(4);  // calls B::foo(int)
obj.foo(4.5); // calls B::foo(double) <-- can we call Derived::foo(T) ?

简而言之,我想要的效果是:

using Base::foo(int);

这可能吗?以上只是一个例子。

最佳答案

using 将所有重载带入作用域。只需将它隐藏在派生类中,它的编写要多一些但可以完成工作:

struct Derived : Base
{
  template<typename T> void foo (T x) {}
  void foo(int x){
    Base::foo(x);
  }
};

关于c++ - "using"关键字是否可以继承较少的功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6149798/

相关文章:

c++ - 在 Windows 7 上静默关闭 VS 2008

c++ - 从多重继承继承

javascript - 为什么 util.inherits 创建一个继承自 super 构造函数原型(prototype)的新对象?

visual-studio-2010 - 将进程附加到 Visual Studio 2010 for Sharepoint 2010 站点中的事件处理程序

c++ - 如何使用 C++ 在 Ubuntu 上强制用户注销?

c++ - 调用 `string::c_str()` 时实际上做了什么?

c++ - 如何实现 MATLAB 与单独的 C++ 应用程序之间的通信?

c++ - 私有(private)成员的继承

c++ - 是否可以将枚举注入(inject)类范围/命名空间?

c# - 是否有任何理由在 AssemblyInfo.cs 中保留对 System.Runtime.CompilerServices 的引用?