Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible for "using" keyword to inherit fewer functions?

In Derived there is a template foo(T). There are 2 overloads of foo() in Base.

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

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

Now, when foo() is called with Derived object; I want to use only Base::foo(int) if applicable, otherwise it should invoke 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) ?

In short, I want an effect of:

using Base::foo(int);

Is it possible ? Above is just an example.

like image 591
iammilind Avatar asked May 27 '11 08:05

iammilind


Video Answer


1 Answers

using bringes all overloads into the scope. Just hide it in the derived class, it's a bit more to write but does the job:

struct Derived : Base
{
  template<typename T> void foo (T x) {}
  void foo(int x){
    Base::foo(x);
  }
};
like image 56
Xeo Avatar answered Nov 15 '22 08:11

Xeo