I have a situation where I need to inherit from two classes with same interface, but to override them separately and I definitely cannot tweak interfaces. See code example below
template<typename T>
struct Foo
{
virtual ~Foo() = default;
virtual void foo() = 0;
};
struct Derived : public Foo<int>, public Foo<double>
{
#if 0 // having something like this would be great, but unfortunately it doesn't work
void Foo<int>::foo() override
{
std::cout << "Foo<int>::foo()" << std::endl;
}
void Foo<double>::foo() override
{
std::cout << "Foo<double>::foo()" << std::endl;
}
#endif
};
CodePudding user response:
You can always define intermediate classes that declare their own interfaces:
template<typename T>
struct Foo
{
virtual ~Foo() = default;
virtual void foo() = 0;
};
struct ProxyFooInt : public Foo<int>
{
virtual void fooInt() = 0;
void foo() override
{
return fooInt();
}
};
struct ProxyFooDouble : public Foo<double>
{
virtual void fooDouble() = 0;
void foo() override
{
return fooDouble();
}
};
struct Derived : public ProxyFooInt, public ProxyFooDouble
{
void fooInt() override
{
std::cout << "Foo<int>::foo()" << std::endl;
}
void fooDouble() override
{
std::cout << "Foo<double>::foo()" << std::endl;
}
};
A more advanced solution would be to use CRTP:
template<typename D>
struct CrtpFooInt : public Foo<int>
{
void foo() override
{
return static_cast<D*>(this)->fooInt();
}
};
template<typename D>
struct CrtpFooDouble : public Foo<double>
{
void foo() override
{
return static_cast<D*>(this)->fooDouble();
}
};
struct Derived : public CrtpFooInt<Derived>, public CrtpFooDouble<Derived>
{
void fooInt()
{
std::cout << "Foo<int>::foo()" << std::endl;
}
void fooDouble()
{
std::cout << "Foo<double>::foo()" << std::endl;
}
};
CodePudding user response:
Another creative way to solve this problem using CRTP would be to use an ability to add some overloading to distinguish between different invocations of foo()
:
template<typename T>
struct Foo
{
virtual ~Foo() = default;
virtual void foo() = 0;
};
template<typename T>
struct Tag{};
template<typename T>
struct Crtp : public Foo<T>
{
virtual void foo(Tag<T>) = 0;
void foo() override
{
foo(Tag<T>{});
}
};
struct Derived : public Crtp<int>, public Crtp<double>
{
void foo(Tag<int>) override
{
std::cout << "Foo<int>::foo()" << std::endl;
}
void foo(Tag<double>) override
{
std::cout << "Foo<double>::foo()" << std::endl;
}
};