Хорошая задача для собеседования "опытных" ребят по С++.
Допустим, у вас есть некий сторонний код интерфейсов/каллбеков, и вам нужно отнаследоваться от всех (в данном случает от двух):
// -- third part code (you can't change this)
Struct IFoo { virtual void Test() = 0; }; // you can't change this
Struct IBla { virtual void Test() = 0; }; // you can't change this
// -- end of third part code
далее нужно реализовать функцию Test для каждого интерфейса отдельно:
// your code (but it does not work rightly)
Struct CTest : public IFoo, public IBla
{
Virtual void Test()
{
std::cout << "I'm a ...?" << std::endl;
}
};
далее опять сторонний код, к примеру такой:
// -- third part code (you can't change this)
Int _tmain(int argc, _TCHAR* argv[])
{
CTest test;
IFoo& foo = test;
foo.Test(); // must be: "I'm a foo" in out
IBla& bla = test;
bla.Test(); // must be: "I'm a bla" in out
return 0;
}
// -- end of third part code
В общем я надеюсь задача ясна - нужно придумать как разрулить ситуацию не меняя интерфейсы и тестовый код.
Решение задачи ниже(белым по белому).
// code code code
#include < iostream >
struct IFoo { virtual void Test() = 0; };
struct IBla { virtual void Test() = 0; };
struct IFooImpl : IFoo
{
virtual void Test() { return FooTest(); }
virtual void FooTest() = 0;
};
struct IBlaImpl : IBla
{
virtual void Test() { return BlaTest(); }
virtual void BlaTest() = 0;
};
struct CTest : public IFooImpl , public IBlaImpl
{
virtual void BlaTest() { std::cout << "I'm a bla" << std::endl; }
virtual void FooTest() { std::cout << "I'm a foo" << std::endl; }
};
int _tmain(int argc, _TCHAR* argv[])
{
CTest test;
IFoo& foo = test;
foo.Test();
IBla& bla = test;
bla.Test();
return 0;
}
// code code code
хорошая задача. да :)
ReplyDelete