[Qt-interest] Non-Virtual Interface Idiom
Robert Escott
robert at binarylogic.co.za
Mon Dec 20 00:55:28 CET 2010
Hi Piotr
I've read the article through a number of times and quite frankly, I don't
really understand what new methodology this guy's talking about.
I don't feel he's argued well enough against the use of public or protected
virtual functions. He mentions the "problem of simultaneity" but I fail to
see why this is a problem at all and the guy doesn't explain it either. He
seems to feel that having public or protected virtual functions somehow
destabilises the class interface but doesn't say how.
If you want rationale behind Qt's decision not to follow the "Non-virtual
interface" paradigm, just read any book on OOP and polymorphism. It's all
there.
Take Qt's Model-View architecture, for example. Views all need to access
data from the various model classes yet the Models need to be customisable
via derived classing. This means that the QVariant data() function has to be
both virtual and public.
Also, all QWidgets and their derived classes must be able to process QEvents
or their derivatives. This means that the event function needs to be
virtual. Since the event function is not triggered from within the QWidget
class itself but inside the event loop, this virtual function has to be
public.
I prefer to program in a practical way and follow the principle of: "Compile
for processors, program for programmers." That's not my principle but what
it means is that coding should be done in a way that is easy to do and also
easy for somebody else to figure out. Providing a secondary public function
that does nothing more than redirect to a private virtual function, although
no extra overhead is involved, is pointless. I see no gain in doing it. On
the other hand, coding ease and readability are negatively affected. Thus
from my point of view, I would avoid this practice. I also don't think this
coding paradigm will catch on much.
-----Original Message-----
From: qt-interest-bounces+robert=binarylogic.co.za at qt.nokia.com
[mailto:qt-interest-bounces+robert=binarylogic.co.za at qt.nokia.com] On Behalf
Of Piotr Dobrogost
Sent: 19 December 2010 21:19
To: qt-interest at qt.nokia.com
Subject: [Qt-interest] Non-Virtual Interface Idiom
One of design rules made popular by Herb Sutter is Non-Virtual Interface
Idiom explained in the article "Virtuality" in C/C++ Users Journal, 19(9),
September 2001 (http://www.gotw.ca/publications/mill18.htm), which can be
summarized in the guideline "Prefer to make interfaces nonvirtual, using
Template Method."
In many places design of Qt classes does not obey this rule. Is there any
rationale for this?
--
Piotr Dobrogost
"Break C++ and the major implementations of Java and C# would also break" -
Bjarne Stroustrup
_______________________________________________
Qt-interest mailing list
Qt-interest at qt.nokia.com
http://lists.qt.nokia.com/mailman/listinfo/qt-interest
More information about the Qt-interest-old
mailing list