Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

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.

Document Tags and Contributors

Tags: 
 Contributors to this page: Sheppy, fscholz, Andreas Wuest, BenB
 Last updated by: Sheppy,