7

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;
};
1
  • You should post the exact error you are getting. Commented May 29, 2011 at 9:10

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

But is there any way to import all methods from Iface1 scope in one statement? Something like using Iface1; does not work.
@barakin, no there is no such method.
2

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.

3 Comments

@Begemoth, I don't think that the code that the OP put as example is the exact code running; otherwise, he/she wouldn't have said that the problem is solved. Your claim is true takeing only into account the exact code given as example.
I don't think OP's made foo in the example virtual by a mistake. Using impl.Iface1::foo in the client code will not be correct and has a rather limited appliance.
Yes, I run into the problem again while linking the code: error about undefined symbols was received. "using::" solved it. But is there any way to import all methods from Iface1 scope in one statement? It looks ugly to write ``using Iface*::method1(), using Iface*::methodN()` etc? using Iface1; does not work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.