If a class inherits multiple classes with the same function, how does it call each inherited class's function without manually specific each class?
Example code as below:
#include <cstdio>
class Interface1
{
public:
virtual ~Interface1() = default;
void foo()
{
printf("%s\n", __PRETTY_FUNCTION__);
}
};
class Interface2
{
public:
virtual ~Interface2() = default;
void foo()
{
printf("%s\n", __PRETTY_FUNCTION__);
}
};
class ObjectWithoutTemplate : public Interface1, Interface2
{
public:
void foo()
{
// How do I write the code to call each InterfaceX's foo() here
// without manually specify each class?
Interface1::foo();
Interface2::foo();
// The desired code looke like
// for each interface in Interfaces {
// interface::foo()
// }
}
};
template <class... Interfaces>
class ObjectWithTemplate : Interfaces...
{
public:
void foo()
{
// It does not compile if the template does not inherit Interface[1|2]
Interface1::foo();
Interface2::foo();
// The desired code looke like
// for each interface in Interfaces {
// interface::foo()
// }
}
};
int main()
{
ObjectWithoutTemplate objWithout;
ObjectWithTemplate<Interface1, Interface2> objWith;
objWithout.foo();
objWith.foo();
return 0;
}
For ObjectWithoutTemplate
, I could call interfaces' foo()
by manually specifying the interface:
Interface1::foo();
Interface2::foo();
But for ObjectWithTemplate
's foo()
, how do I write the code to call each inherited interfaces' foo()
, considering there will be Interface3
, Interface4
as well.