This page describes some workarounds for keeping binary compatibility in patch releases.

I need to add a private method to a class

  • Generally, never add a private method to a class unless you need to call if from public/protected inline functions. Add it to the private class instead.
  • If the class has no private class, declare a static function in the .cpp file that takes a pointer to your class as first argument. If you need to access other private/protected members, make the function a friend.

Example: I need to add a helper function to class A without breaking b/c:

class A
{  
private:  
    friend void myHelper(A *);  
};  

/* in the .cpp file: */  
static void myHelper(A *that)  
{  
...  
}  

I need to add a private slot

Use a Q_PRIVATE_SLOT:

class A: public QObject
{  
    Q_OBJECT  
    ...  
private:  
    Q_PRIVATE_SLOT(d_func(), void myPrivateSlot())  
};  

/* in .cpp file */  

void APrivate::myPrivateSlot() {...}  

\#include "moc_a.cpp"  

Pitfalls to avoid:

  • Don’t include a.moc, but moc_a.cpp in your .cpp file
  • Q_PRIVATE_SLOT takes the complete signature of the private slot, not just its name

11 May 06:59