*** changing any of the types of the arguments in the [[Policies/Binary_Compatibility_Examples#Change_the_parameters_of_a_function|parameter list]], including changing the const/volatile qualifiers of the existing parameters (instead, add a new method)

*** changing any of the types of the arguments in the [[Policies/Binary_Compatibility_Examples#Change_the_parameters_of_a_function|parameter list]], including changing the const/volatile qualifiers of the existing parameters (instead, add a new method)

Line 57:

Line 59:

*** changing the [[Policies/Binary_Compatibility_Examples#Change_the_CV-qualifiers_of_a_member_function|CV-qualifiers of a member function]]: the const and/or volatile that apply to the function itself.

*** changing the [[Policies/Binary_Compatibility_Examples#Change_the_CV-qualifiers_of_a_member_function|CV-qualifiers of a member function]]: the const and/or volatile that apply to the function itself.

*** extending a function with another parameter, even if this parameter has a default argument. ''See below for a suggestion on how to avoid this issue''

*** extending a function with another parameter, even if this parameter has a default argument. ''See below for a suggestion on how to avoid this issue''

+

*** changing the [[Policies/Binary_Compatibility_Examples#Change_the_return_type|return type]] in any way

** [[Policies/Binary_Compatibility_Examples#Override_a_virtual_that_doesn.27t_come_from_a_primary_base|override an existing virtual function if that function is not in the primary base class]] (first non-virtual base class, or the primary base class's primary base class and upwards).

** [[Policies/Binary_Compatibility_Examples#Override_a_virtual_that_doesn.27t_come_from_a_primary_base|override an existing virtual function if that function is not in the primary base class]] (first non-virtual base class, or the primary base class's primary base class and upwards).

** [[Policies/Binary_Compatibility_Examples#Override_a_virtual_with_a_covariant_return_with_different_top_address|override an existing virtual function]] if the overriding function has a [http://en.wikipedia.org/wiki/Covariant_return_type covariant return type] for which the more-derived type has a pointer address different from the less-derived one (usually happens when, between the less-derived and the more-derived ones, there's multiple inheritance or virtual inheritance).

** [[Policies/Binary_Compatibility_Examples#Override_a_virtual_with_a_covariant_return_with_different_top_address|override an existing virtual function]] if the overriding function has a [http://en.wikipedia.org/wiki/Covariant_return_type covariant return type] for which the more-derived type has a pointer address different from the less-derived one (usually happens when, between the less-derived and the more-derived ones, there's multiple inheritance or virtual inheritance).

+

** Remove a virtual function, even if it is a reimplementation of a virtual function from the base class

* For static non-private members or for non-static non-member public data:

* For static non-private members or for non-static non-member public data:

** Remove or unexport it

** Remove or unexport it

Line 76:

Line 80:

If you need to add extend/modify the parameter list of an existing function, you need to add a new function instead with the new parameters. In that case, you may want to add a short note that the two functions shall be merged with a default argument in later versions of the library:

If you need to add extend/modify the parameter list of an existing function, you need to add a new function instead with the new parameters. In that case, you may want to add a short note that the two functions shall be merged with a default argument in later versions of the library:

−

<code cpp>

+

<source lang="cpp-qt">

void functionname( int a );

void functionname( int a );

void functionname( int a, int b ); //BCI: merge with int b = 0

void functionname( int a, int b ); //BCI: merge with int b = 0

−

</code>

+

</source>

You should...

You should...

Line 97:

Line 101:

One exception are bitflags. If you use bitflags for enums or bools, you can safely round up to at least the next byte minus 1. A class with members

One exception are bitflags. If you use bitflags for enums or bools, you can safely round up to at least the next byte minus 1. A class with members

−

<code cpp>

+

<source lang="cpp-qt">

uint m1 : 1;

uint m1 : 1;

uint m2 : 3;

uint m2 : 3;

uint m3 : 1;

uint m3 : 1;

−

</code>

+

</source>

−

<code cpp>

+

<source lang="cpp-qt">

uint m1 : 1;

uint m1 : 1;

uint m2 : 3;

uint m2 : 3;

uint m3 : 1;

uint m3 : 1;

uint m4 : 2; // new member

uint m4 : 2; // new member

−

</code>

+

</source>

without breaking binary compatibility. Please round up to a maxmimum of 7 bits (or 15 if the bitfield was already larger than 8). Using the very last bit may cause problems on some compilers.

without breaking binary compatibility. Please round up to a maxmimum of 7 bits (or 15 if the bitfield was already larger than 8). Using the very last bit may cause problems on some compilers.

All you have to do now is to create the private data in your constructors or your init function with

All you have to do now is to create the private data in your constructors or your init function with

−

<code cpp>

+

<source lang="cpp-qt">

d = new FooPrivate;

d = new FooPrivate;

−

</code>

+

</source>

and to delete it again in your destructor with

and to delete it again in your destructor with

−

<code cpp>

+

<source lang="cpp-qt">

delete d;

delete d;

−

</code>

+

</source>

In most circumstances you will want to make the dpointer constant to catch situations where it's accidentally getting modified or copied over so you'd loose ownership of the private object and create a memory-leak:

In most circumstances you will want to make the dpointer constant to catch situations where it's accidentally getting modified or copied over so you'd loose ownership of the private object and create a memory-leak:

−

<code cpp>

+

<source lang="cpp-qt">

private:

private:

FooPrivate* const d;

FooPrivate* const d;

−

</code>

+

</source>

This allows you to modify the object pointed to by d but not the value of the pointer after it has been initialized.

This allows you to modify the object pointed to by d but not the value of the pointer after it has been initialized.

You may not want all member variables to live in the private data object, though. For very often used members, it's faster to put them directly in the class, since inline functions cannot access the d-pointer data. Also note that all data covered by the d-pointer is obviously private. For public or protected access, provide both a set and a get function. Example

You may not want all member variables to live in the private data object, though. For very often used members, it's faster to put them directly in the class, since inline functions cannot access the d-pointer data. Also note that all data covered by the d-pointer is obviously private. For public or protected access, provide both a set and a get function. Example

−

<code cppqt>

+

<source lang="cpp-qt">

QString Foo::string() const

QString Foo::string() const

{

{

Line 167:

Line 171:

d->s = s;

d->s = s;

}

}

−

</code>

+

</source>

<h2>Trouble shooting</h2>

<h2>Trouble shooting</h2>

Line 178:

Line 182:

The basic trick in your class implementation of class Foo is:

The basic trick in your class implementation of class Foo is:

* Create a private data class FooPrivate.

* Create a private data class FooPrivate.

−

* Create a static QHash&lt;Foo *, FooPrivate&gt;.

+

* Create a static QHash&lt;Foo *, FooPrivate *&gt;.

*Note that some compilers/linkers (almost all, unfortunately) do not manage to create static objects in shared libraries. They simply forget to call the constructor. Therefore you should use the <tt>Q_GLOBAL_STATIC</tt> macro to create and access the object:

*Note that some compilers/linkers (almost all, unfortunately) do not manage to create static objects in shared libraries. They simply forget to call the constructor. Therefore you should use the <tt>Q_GLOBAL_STATIC</tt> macro to create and access the object:

−

<code cppqt>

+

<source lang="cpp-qt">

// BCI: Add a real d-pointer

// BCI: Add a real d-pointer

−

Q_GLOBAL_STATIC(QHash<Foo *,FooPrivate>, d_func);

+

Q_GLOBAL_STATIC(QHash<Foo *,FooPrivate *>, d_func);

static FooPrivate* d( const Foo* foo )

static FooPrivate* d( const Foo* foo )

{

{

Line 199:

Line 203:

d_func()->remove( foo );

d_func()->remove( foo );

}

}

−

</code>

+

</source>

* Now you can use the d-pointer in your class almost as simple as in the code before, just with a function call to d(this). For example:

* Now you can use the d-pointer in your class almost as simple as in the code before, just with a function call to d(this). For example:

−

<code cppqt>

+

<source lang="cpp-qt">

d(this)->m1 = 5;

d(this)->m1 = 5;

−

</code>

+

</source>

* Add a line to your destructor:

* Add a line to your destructor:

−

<code cppqt>

+

<source lang="cpp-qt">

delete_d(this);

delete_d(this);

−

</code>

+

</source>

* Do not forget to add a BCI remark, so that the hack can be removed in the next version of the library.

* Do not forget to add a BCI remark, so that the hack can be removed in the next version of the library.

* Do not forget to add a d-pointer to your next class.

* Do not forget to add a d-pointer to your next class.

Line 217:

Line 221:

As already explained, you can safely reimplement a virtual function defined in one of the base classes only if it is safe that the programs linked with the prior version call the implementation in the base class rather than the new one. This is because the compiler sometimes calls virtual functions directly if it can determine which one to call. For example, if you have

As already explained, you can safely reimplement a virtual function defined in one of the base classes only if it is safe that the programs linked with the prior version call the implementation in the base class rather than the new one. This is because the compiler sometimes calls virtual functions directly if it can determine which one to call. For example, if you have

−

<code cpp>

+

<source lang="cpp-qt">

void C::foo()

void C::foo()

{

{

B::foo();

B::foo();

}

}

−

</code>

+

</source>

then B::foo() is called directly. If class B inherits from class A which implements foo() and B itself doesn't reimplement it, then C::foo() will in fact call A::foo(). If a newer version of the library adds B::foo(), C::foo() will call it only after a recompilation.

then B::foo() is called directly. If class B inherits from class A which implements foo() and B itself doesn't reimplement it, then C::foo() will in fact call A::foo(). If a newer version of the library adds B::foo(), C::foo() will call it only after a recompilation.

Another more common example is:

Another more common example is:

−

<code cpp>

+

<source lang="cpp-qt">

B b; // B derives from A

B b; // B derives from A

b.foo();

b.foo();

−

</code>

+

</source>

then the call to foo() will not use the virtual table. That means that

then the call to foo() will not use the virtual table. That means that

if B::foo() didn't exist in the library but now does, code that was

if B::foo() didn't exist in the library but now does, code that was

Line 236:

Line 240:

If you can't guarantee things will continue to work without a recompilation, move functionality from A::foo() to a new protected function A::foo2() and use this code:

If you can't guarantee things will continue to work without a recompilation, move functionality from A::foo() to a new protected function A::foo2() and use this code:

−

<code cpp>

+

<source lang="cpp-qt">

void A::foo()

void A::foo()

{

{

Line 249:

Line 253:

A::foo2(); // call base function with real functionality

A::foo2(); // call base function with real functionality

}

}

−

</code>

+

</source>

All calls to A::foo() for objects of type B (or inherited) will result in calling B::foo(). The only case that will not work as expected are calls to A::foo() that explicitly specify A::foo(), but B::foo() calls A::foo2() instead and there should not be other places doing so.

All calls to A::foo() for objects of type B (or inherited) will result in calling B::foo(). The only case that will not work as expected are calls to A::foo() that explicitly specify A::foo(), but B::foo() calls A::foo2() instead and there should not be other places doing so.

Line 258:

Line 262:

=== Adding new virtual functions to leaf classes ===

=== Adding new virtual functions to leaf classes ===

This technique is one of cases of using a new class that can help if there's a need to add new virtual functions to a class that should stay binary compatible and there is no class inheriting from it that should also stay binary compatible (i.e. all classes inheriting from it are in applications). In such case it's possible to add a new class inheriting from the original one that will add them. Applications using the new functionality will of course have to be modified to use the new class.

This technique is one of cases of using a new class that can help if there's a need to add new virtual functions to a class that should stay binary compatible and there is no class inheriting from it that should also stay binary compatible (i.e. all classes inheriting from it are in applications). In such case it's possible to add a new class inheriting from the original one that will add them. Applications using the new functionality will of course have to be modified to use the new class.

−

<code cpp>

+

<source lang="cpp-qt">

class A {

class A {

public:

public:

Line 273:

Line 277:

this2->bar();

this2->bar();

}

}

−

</code>

+

</source>

It is not possible to use this technique when there are other inherited classes that should also stay binary compatible because they'd have to inherit from the new class.

It is not possible to use this technique when there are other inherited classes that should also stay binary compatible because they'd have to inherit from the new class.

Line 279:

Line 283:

Qt's signals and slots are invoked using a special virtual method created by the Q_OBJECT macro and it exists in every class inherited from {{qt|QObject}}. Therefore adding new signals and slots doesn't affect binary compatibility and the signals/slots mechanism can be used to emulate virtual functions.

Qt's signals and slots are invoked using a special virtual method created by the Q_OBJECT macro and it exists in every class inherited from {{qt|QObject}}. Therefore adding new signals and slots doesn't affect binary compatibility and the signals/slots mechanism can be used to emulate virtual functions.

−

<code cppqt>

+

<source lang="cpp-qt">

class A : public QObject {

class A : public QObject {

Q_OBJECT

Q_OBJECT

Line 307:

Line 311:

*ret = 10;

*ret = 10;

}

}

−

</code>

+

</source>

Function bar() will act like a virtual function, barslot() implements the actual functionality of it. Since signals have void return value, data must be returned using arguments. As there will be only one slot connected to the signal returning data from the slot this way will work without problems. Note that with Qt4 for this to work the connection type will have to be Qt::DirectConnection.

Function bar() will act like a virtual function, barslot() implements the actual functionality of it. Since signals have void return value, data must be returned using arguments. As there will be only one slot connected to the signal returning data from the slot this way will work without problems. Note that with Qt4 for this to work the connection type will have to be Qt::DirectConnection.

If an inherited class will want to re-implement the functionality of bar() it will have to provide its own slot:

If an inherited class will want to re-implement the functionality of bar() it will have to provide its own slot:

−

<code cppqt>

+

<source lang="cpp-qt">

class B : public A {

class B : public A {

Q_OBJECT

Q_OBJECT

Line 331:

Line 335:

*ret = 20;

*ret = 20;

}

}

−

</code>

+

</source>

Now B::barslot() will act like virtual reimplementation of A::bar(). Note that it is necessary to specify barslot() again as a slot in B and that in the constructor it is necessary to first disconnect and then connect again, that will disconnect A::barslot() and connect B::barslot() instead.

Now B::barslot() will act like virtual reimplementation of A::bar(). Note that it is necessary to specify barslot() again as a slot in B and that in the constructor it is necessary to first disconnect and then connect again, that will disconnect A::barslot() and connect B::barslot() instead.

Definition

A library is binary compatible, if a program linked dynamically to a former version of the library continues running with newer versions of the library without the need to recompile.

If a program needs to be recompiled to run with a new version of library but doesn't require any further modifications, the library is source compatible.

Binary compatibility saves a lot of trouble. It makes it much easier to distribute software for a certain platform. Without ensuring binary compatibility between releases, people will be forced to provide statically linked binaries. Static binaries are bad because they

waste resources (especially memory)

don't allow the program to benefit from bugfixes or extensions in the libraries

In the KDE project, we will provide binary compatibility within the life-span of a major release for the core libraries (kdelibs, kdepimlibs).

Note about ABI

This text applies to most C++ ABIs used by compilers which KDE can be built with. It is mostly based on the Itanium C++ ABI, which is used by the GCC C++ compiler since version 3.4 in all platforms it supports. Information about Microsoft Visual C++ mangling scheme mostly comes from this article on calling conventions (it's the most complete information found so far on MSVC ABI and name mangling).

Some of the constraints specified here may not apply to a given compiler. The goal here is to list the most restrictive set of conditions when writing cross-platform C++ code, meant to be compiled with several different compilers.

This page is updated when new binary incompatibility issues are found.

The Do's and Don'ts

You can...

add new non-virtual functions including signals and slots and constructors.

add a new enum to a class.

append new enumerators to an existing enum.

reimplement virtual functions defined in the primary base class hierarchy (that is, virtuals defined in the first non-virtual base class, or in that class's first non-virtual base class, and so forth) if it is safe that programs linked with the prior version of the library call the implementation in the base class rather than the new one. This is tricky and might be dangerous. Think twice before doing it. Alternatively see below for a workaround.

Exception: if the overriding function has a covariant return type, it's only a binary-compatible change if the more-derived type has always the same pointer address as the less-derived one. If in doubt, do not override with a covariant return type.

change an inline function or make an inline function non-inline if it is safe that programs linked with the prior version of the library call the old implementation. This is tricky and might be dangerous. Think twice before doing it.

remove private non-virtual functions if they are not called by any inline functions (and have never been).

remove private static members if they are not called by any inline functions (and have never been).

add new static data members.

change the default arguments of a method. It requires recompilation to use the actual new default argument values, though.

Remove the implementation of existing declared functions. The symbol comes from the implementation of the function, so this is effectively the function.

inline it (this includes moving a member function's body to the class definition, even without the inline keyword).

add an overload (BC, but not SC: makes &func ambiguous), adding overloads to already overloaded functions is ok (any use of &func already needed a cast).

change its signature. This includes:

changing any of the types of the arguments in the parameter list, including changing the const/volatile qualifiers of the existing parameters (instead, add a new method)

changing the const/volatile qualifiers of the function

changing the access rights to some functions or data members, for example from private to public. With some compilers, this information may be part of the signature. If you need to make a private function protected or even public, you have to add a new function that calls the private one.

override an existing virtual function if the overriding function has a covariant return type for which the more-derived type has a pointer address different from the less-derived one (usually happens when, between the less-derived and the more-derived ones, there's multiple inheritance or virtual inheritance).

Remove a virtual function, even if it is a reimplementation of a virtual function from the base class

For static non-private members or for non-static non-member public data:

If you need to add extend/modify the parameter list of an existing function, you need to add a new function instead with the new parameters. In that case, you may want to add a short note that the two functions shall be merged with a default argument in later versions of the library:

Techniques for Library Programmers

The biggest problem when writing libraries is, that one cannot safely add data members since this would change the size and layout of every class, struct, or array containing objects of the type, including subclasses.

Bitflags

One exception are bitflags. If you use bitflags for enums or bools, you can safely round up to at least the next byte minus 1. A class with members

uintm1:1;uintm2:3;uintm3:1;

uintm1:1;uintm2:3;uintm3:1;uintm4:2;// new member

without breaking binary compatibility. Please round up to a maxmimum of 7 bits (or 15 if the bitfield was already larger than 8). Using the very last bit may cause problems on some compilers.

Using a d-Pointer

Bitflags and predefined reserved variables are nice, but far from being sufficient. This is where the d-pointer technique comes into play. The name "d-pointer" stems from Trolltech's Arnt Gulbrandsen, who first introduced the technique into Qt, making it one of the first C++ GUI libraries to maintain binary compatibility even between bigger release. The technique was quickly adapted as general programming pattern for the KDE libraries by everyone who saw it. It's a great trick to be able to add new private data members to a class without breaking binary compatibility.

Remark: The d-pointer pattern has been described many
times in computer science history under various names, e.g. as pimpl,
as handle/body or as cheshire cat. Google helps finding online papers
for any of these, just add C++ to the search terms.

All you have to do now is to create the private data in your constructors or your init function with

d=newFooPrivate;

and to delete it again in your destructor with

deleted;

In most circumstances you will want to make the dpointer constant to catch situations where it's accidentally getting modified or copied over so you'd loose ownership of the private object and create a memory-leak:

private:FooPrivate*constd;

This allows you to modify the object pointed to by d but not the value of the pointer after it has been initialized.

You may not want all member variables to live in the private data object, though. For very often used members, it's faster to put them directly in the class, since inline functions cannot access the d-pointer data. Also note that all data covered by the d-pointer is obviously private. For public or protected access, provide both a set and a get function. Example

Trouble shooting

Adding new data members to classes without d-pointer

If you don't have free bitflags, reserved variables and no d-pointer either, but you absolutely have to add a new private member variable, there are still some possibilities left. If your class inherits QObject, you can for example place the additional data in a special child and find it by traversing over the list of children. You can access the list of children with QObject::children(). However, a fancier and usually faster approach is to use a hashtable to store a
mapping between your object and the extra data. For this purpose, Qt provides a pointer-based dictionary called QHash (or QPtrDict in Qt3).

The basic trick in your class implementation of class Foo is:

Create a private data class FooPrivate.

Create a static QHash<Foo *, FooPrivate *>.

Note that some compilers/linkers (almost all, unfortunately) do not manage to create static objects in shared libraries. They simply forget to call the constructor. Therefore you should use the Q_GLOBAL_STATIC macro to create and access the object:

Now you can use the d-pointer in your class almost as simple as in the code before, just with a function call to d(this). For example:

d(this)->m1=5;

Add a line to your destructor:

delete_d(this);

Do not forget to add a BCI remark, so that the hack can be removed in the next version of the library.

Do not forget to add a d-pointer to your next class.

Adding a reimplemented virtual function

As already explained, you can safely reimplement a virtual function defined in one of the base classes only if it is safe that the programs linked with the prior version call the implementation in the base class rather than the new one. This is because the compiler sometimes calls virtual functions directly if it can determine which one to call. For example, if you have

voidC::foo(){B::foo();}

then B::foo() is called directly. If class B inherits from class A which implements foo() and B itself doesn't reimplement it, then C::foo() will in fact call A::foo(). If a newer version of the library adds B::foo(), C::foo() will call it only after a recompilation.

Another more common example is:

Bb;// B derives from Ab.foo();

then the call to foo() will not use the virtual table. That means that
if B::foo() didn't exist in the library but now does, code that was
compiled with the earlier version will still call A::foo().

If you can't guarantee things will continue to work without a recompilation, move functionality from A::foo() to a new protected function A::foo2() and use this code:

All calls to A::foo() for objects of type B (or inherited) will result in calling B::foo(). The only case that will not work as expected are calls to A::foo() that explicitly specify A::foo(), but B::foo() calls A::foo2() instead and there should not be other places doing so.

Using a new class

A relatively simple method of "extending" a class can be writing a replacement class that will include also the new functionality (and that may inherit from the old class to reuse the code). This of course requires adapting and recompiling applications using the library, so it is not possible this way to fix or extend functionality of classes that are used by applications compiled against an older version of the library. However, especially with small and/or performance-critical classes it may be simpler to write them without making sure they'll be simple to extend in the future and if the need arises later write a new replacement class that will provide new features or better performance.

Adding new virtual functions to leaf classes

This technique is one of cases of using a new class that can help if there's a need to add new virtual functions to a class that should stay binary compatible and there is no class inheriting from it that should also stay binary compatible (i.e. all classes inheriting from it are in applications). In such case it's possible to add a new class inheriting from the original one that will add them. Applications using the new functionality will of course have to be modified to use the new class.

It is not possible to use this technique when there are other inherited classes that should also stay binary compatible because they'd have to inherit from the new class.

Using signals instead of virtual functions

Qt's signals and slots are invoked using a special virtual method created by the Q_OBJECT macro and it exists in every class inherited from QObject. Therefore adding new signals and slots doesn't affect binary compatibility and the signals/slots mechanism can be used to emulate virtual functions.

classA:publicQObject{Q_OBJECTpublic:A();virtualvoidfoo();signals:voidbar(int*);// added new "virtual" functionprotectedslots:// implementation of the virtual function in Avoidbarslot(int*);};A::A(){connect(this,SIGNAL(bar(int*)),this,SLOT(barslot(int*)));}voidA::foo(){intret;emitbar(&ret);}voidA::barslot(int*ret){*ret=10;}

Function bar() will act like a virtual function, barslot() implements the actual functionality of it. Since signals have void return value, data must be returned using arguments. As there will be only one slot connected to the signal returning data from the slot this way will work without problems. Note that with Qt4 for this to work the connection type will have to be Qt::DirectConnection.

If an inherited class will want to re-implement the functionality of bar() it will have to provide its own slot:

classB:publicA{Q_OBJECTpublic:B();protectedslots:// necessary to specify as a slot againvoidbarslot(int*);// reimplemented functionality of bar()};B::B(){disconnect(this,SIGNAL(bar(int*)),this,SLOT(barslot(int*)));connect(this,SIGNAL(bar(int*)),this,SLOT(barslot(int*)));}voidB::barslot(int*ret){*ret=20;}

Now B::barslot() will act like virtual reimplementation of A::bar(). Note that it is necessary to specify barslot() again as a slot in B and that in the constructor it is necessary to first disconnect and then connect again, that will disconnect A::barslot() and connect B::barslot() instead.