Question

Let's say we have two C++/CLI interfaces declaring Foo() members with different return type.

public interface class InterfaceA
{
    bool Foo();
};

public interface class InterfaceB
{
    int Foo();
};

What we want to do here is to have a class that instantiates an object that can be accessed through the above interfaces. So, the straight forward way to do that would be:

public ref class Class : InterfaceA, InterfaceB
{
    virtual bool Foo() = InterfaceA::Foo { return true; }
    virtual int Foo() = InterfaceB::Foo { return 10; }
};

Unfortunately that gives us compiler error "overloaded function differs only by return type from". Is there any workaround for this C++/CLI limitation?

Was it helpful?

Solution

No, you have to rename the method. For example:

public ref class Class : InterfaceA, InterfaceB
{
public:
    virtual bool Foo() { return true; }
    virtual int Foo2() = InterfaceB::Foo { return 10; }
};

Note how this is never a real problem. If code has a reference to Class instead of the interface for some reason then it can always call InterfaceB::Foo() with a cast:

Class^ obj = gcnew Class;
safe_cast<InterfaceB^>(obj)->Foo();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top