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 interfaces).

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据

词条统计

浏览:67 次

字数:2851

最后编辑:7年前

编辑次数:0 次

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文