signals and slots vs. virtual_hook (was [PATCH] KFileDialog overwrite confirmation)

Thiago Macieira thiago at kde.org
Tue Jul 15 03:57:51 BST 2008


Rafael Fernández López wrote:
>What do you think ? Am I just crazy ?

The problem with the "enum solution" is that you need to modify the most 
basic class in the hierarchy, not just the classes affected. Here's it 
with code:

1) There's BaseClass, where virtual_hook is defined initially
2) There's MiddleClass: public BaseClass
3) There's DerivedClass: public MiddleClass
4) We decide that MiddleClass needs a new public, virtual function. We 
implement that by way of a non-virtual public and by overriding the 
virtual hook in MiddleClass and in DerivedClass.
5) Where did the enum get defined?
  5.a) BaseClass, which means you must modify the most basic class in the 
hierarchy, which sometimes you can't or don't want to
  5.b) MiddleClass, which is the "proper place"

However, if you do modify MiddleClass, what happens when BaseClass needs a 
virtual? It has to be sure not to use the same enum value that was used 
in MiddleClass -- wherever that middle class ended up being.

Besides, my point is exactly that qt_metacall *is* the same as 
virtual_hook. There's no need to involve signals here. Technically all 
you needed to do was:

void MiddleClass::publicFunction(bool b)
{
    void *args[] = { &b };
    int id = metaObject()->indexOfMethod("privateFunction(bool)");
    qt_metacall(QMetaObject::InvokeMetaMethod, id, args);
}

In other words, the enum gets replaced by the indexOfMethod call. That 
guarantees that you find the most derived implementation. That also 
guarantees that there will be no clash by adding more methods in the 
hierarchy.

Finally, and this is the strongest argument of all: KDE 4's API was 
designed with this recommendation in mind. Let's not change it now.

By the way, the recommended, easy way is this:

class MiddleClass: public BaseClass
{
    Q_OBJECT

public:
    void publicFunction(bool b);
protected:
    Q_INVOKABLE void privateFunction(bool b);
};

void MiddleClass::publicFunction(bool b)
{
    QMetaObject::invokeMethod(this, "privateFunction", Q_ARG(bool, b));
}

-- 
  Thiago Macieira  -  thiago (AT) macieira.info - thiago (AT) kde.org
    PGP/GPG: 0x6EF45358; fingerprint:
    E067 918B B660 DBD1 105C  966C 33F5 F005 6EF4 5358
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.kde.org/pipermail/kde-core-devel/attachments/20080714/a6f8fb77/attachment.sig>


More information about the kde-core-devel mailing list