Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Methods overloading while inheritance in C++

I have a legacy code:

struct Iface1
{
  virtual ~Iface1() {}
  virtual void foo(const int arg1) const = 0;
};

struct Iface2
{
  virtual ~Iface2() {}
  virtual void foo(const int arg1, const int arg2) const = 0;
};

/// Composite interface
struct Iface12 : Iface1, Iface2
{
};

I need to create a decorator for composite interface. The following code even is not compiled since it is "ambiguous" for G++ and MSVC to deduce which type of foo() is called. Could please anyone point me out how to make the code below compile and work? (unfortunately I have no time for refactoring).

And I even do not understand why the compiler cannot deduce what function to call since all function signatures are explicit. Thanks.

struct IfaceDecorator : Iface12
{
  IfaceDecorator(Iface12& iface) : impl(iface) {}

  virtual void foo(const int arg1) const
  {
    impl.foo(arg1);
  }

 virtual void foo(const int arg1, const int arg2) const
 {
   impl.foo(arg1, arg2);
 }

private:
  Iface12& impl;
};
like image 320
barankin Avatar asked May 29 '11 09:05

barankin


People also ask

Can we have method overloading in inheritance?

Yes of course, overloading in inheritance class is possible in Java. Java compiler detect that add method has multiple implementations. so according to the parameter java compiler will determines which method has to be executed. class Parent { public void add(int a) { System.

Can we use method overloading in inheritance in C#?

You can override the functionality of a base class method to create a same name method with same signature in a derived class. You can achieve method overriding using inheritance.

Can method overloading be done in C?

This feature is present in most of the Object Oriented Languages such as C++ and Java. But C doesn't support this feature not because of OOP, but rather because the compiler doesn't support it (except you can use _Generic).

Does overloading requires an inheritance relationship?

The concepts of overloading and inheritance are not really related. You are defining the method f to take either a double or an int (overloading). The compiler decides which method to call based on the parameters.


2 Answers

You need explicitly import foo into the Iface12 class, so you'll get two overloaded functions Iface12::foo.

struct Iface12 : Iface1, Iface2
{
  using Iface1::foo;
  using Iface2::foo;
};

The member functions with the same name overload each other only when they are declared in the same class, if you want to overload an inherited function you need to import the name in the current class via using ParentClass::functionName. I think this is principle of least surprise - your member function will not be overloaded unless you ask for it.

like image 58
Begemoth Avatar answered Oct 04 '22 08:10

Begemoth


If the problem is in the message to impl, you can fix it with:

impl.Iface1::foo( arg1 );
// ...
impl.Iface2::foo( arg1, arg2 );

... and so on.

EDITED: I've tested it and the ambiguity is over.

like image 41
Baltasarq Avatar answered Oct 04 '22 09:10

Baltasarq