Inheriting from implementation classes 编辑
Given that IDL interfaces map to abstract classes in C++, a common problem when dealing with IDL is when you have an IDL inheritance hierarchy, and a corresponding C++ implementation hierarchy, you run into multiple inheritance. That's not inherently multiple inheritance, though, because you only mix in interfaces (i.e. the problem would not exist with Java's interface
s).
Example problem
You have interfaces A and B, B inherits from A, and you have implementation classes for both A and B, and you want the implementation class for B to inherit from the implementation class for A. The latter is the problem. The lacking virtual
in the generated IB C++ abstract class doesn't make things easier. So, in code:
IDL:
interface IA { void funcA(); } interface IB : IA { void funcB(); } interface IC : IB { void funcC(); }
C++ implementation:
class A : public IA { ??? } class B : public A, public IB { ??? } class C : public B, public IC { ??? }
Example solution
A.h, as usual: class A : public IA { ... NS_DECL_ISUPPORTS NS_DECL_IA } A.cpp, as usual: NS_IMPL_ISUPPORTS1(A, IA) NS_IMETHODIMP A::funcA() { ... } B.h: class B : public A, public IB { ... NS_DECL_ISUPPORTS_INHERITED NS_FORWARD_IA(A::) // need to disambiguate NS_DECL_IB } B.cpp: NS_IMPL_ISUPPORTS_INHERITED1(B, A, IB) // this, superclass,added ifaces NS_IMETHODIMP B::funcB() { ... } C.h: class C : public B, public IC { ... NS_DECL_ISUPPORTS_INHERITED NS_FORWARD_IA(B::) NS_FORWARD_IB(B::) NS_DECL_IC } C.cpp: NS_IMPL_ISUPPORTS_INHERITED1(C, B, IC) NS_IMETHODIMP C::funcC() { ... }
Threadsafe
If you want to make this threadsafe, you can replace NS_IMPL_ISUPPORTS1(A, IA)
with NS_IMPL_THREADSAFE_ISUPPORTS1(A, IA)
in the base implementation class only (e.g. A here). The rest stays as-is. The inheriting classes (B, C here) don't need to change their macros.
See also
Author Ben Bucksch.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论