Re: Something like a final method
* Marcel M?ller:
James Kanze wrote:
Right pattern, but you've inverted the actions:
class Interface
{
public:
int getStatus() const
{
return doGetStatus() ;
}
private:
virtual int doGetStatus() const = 0 ;
} ;
class AbstractBase : public Interface
{
public:
int getStatus() const
{
return currentStatus ;
}
private:
virtual int doGetStatus() const
{
return getStatus() ;
}
private:
int myStatus ;
} ;
Thanks! This is the best suggestion so far. The syntax for the caller is
symmetric without a performance impact. I don't know why something like
that did not come into my mind.
Note that James' code is essentially the same as blaarg's.
And what it means that
o.getStatus()
may perform a virtual or a non-virtual call depending on the type of 'o'.
The code I presented does not suffer from that problem.
It's a fairly standard pattern, I think.
At least I did not use it so far.
The pattern used in the Interface is standard, in the sense of being quite
common and established as "good" (it's a shame it can't be used with Java).
The inversion of that pattern in AbstractBase, in order to avoid virtual calls,
is not standard.
It means that (as with the code I presented, and any solution, but it's in your
requirements so can't be avoided)
1. the wrong implementation of getStatus can easily be called.
And as noted above (not the case with the code I presented) it means that
2a. one can not see by local inspection of call whether a call is non-virtual
or not, and worse,
2b. if you have the wrong static type for an object, e.g.
AbstractBase& where the dynamic type is DerivedWithCustomStatus, then a
cast is needed to get the correct virtual call.
Finally, (not the case with the code I presented)
3. by adding the status implementation directly in AbstractBase that
implementation is forced as baggage on derived classes using another
implementation,
which presumably there will be, otherwise the status implementation can and
should be hoisted up in Interface (which should then perhaps be called something
else).
Cheers & hth.,
- Alf
--
Due to hosting requirements I need visits to <url: http://alfps.izfree.com/>.
No ads, and there is some C++ stuff! :-) Just going there is good. Linking
to it is even better! Thanks in advance!