Sharing interface implementation among impl. of derived interfaces
Hi, folks!
I have a problem sharing common implementation of an interface among
implementations of derived interfaces.
I have three interfaces (abstract classes) in my program: Node, File
and Folder. File and Folder are Node-s themselves, so they are
publicly derived from Node. This cannot be changed and I have to live
with it.
Next, Node interface have methods common to File and Folder. File and
Folder interfaces add methods specific for files and folders.
I only need to have one implementation of File abstract class, say
FileImpl. And one implementation of Folder-- FolderImpl. If I
implement Node interface in a class, say NodeImpl, then how should I
implement FileImpl and FolderImpl?
It is clear that simply deriving FileImpl from NodeImpl does not brings
in File's methods, but deriving additionally from File leaves
unimplemented Node sub-object methods in that File. The same is for
FolderImpl.
I have some options now:
1. Include NodeImpl as a private data member into File and Folder
classes, then delegate Node's method calls to this data member. (which
is ugly)
2. Inherit File and Folder from Node using virtual keyword-- no
delegation will be need, but, argh!.. environment restrictions do not
let me do that (I must conform to COM and virtual inheritance of
interfaces is not allowed).
3. Make NodeImpl a class template taking the class name as a parameter
and make it inherit from that interface like this:
template< typename T >
class NodeImpl: public T
{
//...
};
Then derive FileImpl and FolderImpl like this:
class FileImpl: public NodeImpl< File >
{
//...
};
class FolderImpl: public NodeImpl< Folder >
{
//...
};
This latter one is quite tempting since it works and does not need
delegation of calls to Node methods-- I only need to add implementation
of file- and folder- specific methods :)
But the question arises: is there a guarantee that the code for
NodeImpl< File > and NodeImpl< Folder > specializations will be shared
or does this solely depend on the quality of compiler? Please note,
that in my case NodeImpl knows nothing about File or Folder and never
uses it's template parameter T directly. I assume that specific
template parameter affects specialized template class' virtual table
layout, but not the layout of data members. Thus the same code can be
generated (and used) for both NodeImpl< File > and NodeImpl< Folder >.
Am I right? If so, is there any drawback of such an approach?
4. Better alternatives are welcome!
Thanks.
Alex
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]