如果我有以下代码:
class A_Interface { public: virtual void i_am_a() = 0; };
class A : public A_Interface {
public:
void i_am_a() {printf("This is A\n");}
};
class B_interface {
public:
virtual void i_am_b() = 0;
// would like to run i_am_a()
};
class B : public A, public B_interface {
public:
void i_am_b() {printf("This is B\n");}
};
int main() {
B BO;
B_interface* BI = &BO;
BI->i_am_b();
// ******* WOULD LIKE TO RUN THE FOLLOWING ********
BI->i_am_a();
}
有什么选择能够从B_Interface指针运行A类成员函数?
我知道B_interface可以将A_interface继承为:
class B_Interface:virtual public A_interface ...
class A : virtural public A_Interface ...
但是这使我无法根据自己的知识使用GMOCK来模拟B课。我有什么选择?
...谢谢
答案 0 :(得分:1)
内部class B_interface
有:
void i_am_a()
{
// throws if the object does not actually have A as a base
dynamic_cast<A &>(*this).i_am_a();
}
答案 1 :(得分:0)
好的,我错了,再说:(。所以答案代码可以是这样的:
class A_Interface {
public:
virtual void i_am_a() = 0;
};
class A : virtual public A_Interface {
public:
void i_am_a() {printf("This is A\n");}
};
class B_Interface : virtual public A_Interface {
public:
virtual void i_am_b() = 0;
// would like to run i_am_a()
};
class B : public A, public B_interface {
public:
void i_am_b() {printf("This is B\n");}
};
GMock可以是这样的:
class MockB : public B_Interface, public MockA {
MOCK_METHOD0(foo, int(int));
}
这在实际应用中对我有用,但看起来有点乱。