On Jan 4, 6:52 pm, "Alf P. Steinbach" <al...@start.nowrote:
* mmcgarry.w...@gmail.com:
I would like to follow Stroustrup's advice of separating an object
interface (abstract class) from an object implementation (concrete
class), See Section 15.2.5 in Stroustrup 3rd Edition.
Specifically, I want to create an abstract base class that defines an
object interface:
class myAbstractClass
{
virtual func1() = 0;
virtual func2() = 0;
virtual func3() = 0;
};
I would then have one or more concrete classes that override these
virtual functions to implement this interface.
class usefulClass
: public myAbstractClass // interface
, protected myImplClass1 // implementation of func1() and func2()
, protected myImpClass2 // implementation of func3()
{
...
};
The standard mixin pattern, in sum. Except that in the
classical idiom 1) the implementation classes would generally
inherit from the interface as well, so virtual inheritance is
called for, and 2) the inheritance of the implementation classes
should be private, not protected.
Essentially usefulClass glues together implementation classes that
fill out the interface defined by the abstract class. I would like to
know how to quantify the run-time overhead for this? Is it greater
then the run-time overhead of just inheriting a single concrete base
class?
The way you write it, using wrapper functions, no, a wrapper
has the same overhead anyway.
The way he writes it is probably preferable for simple cases,
but it doesn't always work. Suppose that func1() needs to call
func3() in some cases.
Letting the compiler tie the implementations to the interface, i.e.
using virtual inheritance (Java-style implementation inheritance), you
may incur some slight overhead because a diamond inheritance requires
more complicated virtual function address lookup.
Just a nit: the virtual function address lookup is exactly the
same. There is often a small performance loss because the
calculation of the this pointer is slightly more complicated,
and there is also often a small additional space overhead as
well.
To quantify this possible overhead in the second case, measure.
By the way, it's generally not a good idea to split
implementations of a single interface in different classes.
That indicates a design error either for the interface or the
implementation classes or both.
In a lot of cases, perhaps. There are cases where the mixin
pattern is just the thing, however. It's a natural when the
"customization" can be categorized in several dimensions.
--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34