Difference between revisions of "Policies/Binary Compatibility Examples"

Jump to: navigation, search
(Add a section about changing CV-qualifiers)
(Add two examples of BC break when changing constness of parameters)
Line 179: Line 179:
 
| <code cppqt>
 
| <code cppqt>
 
void doSomething(int i1, int i2, int i3 = 0);
 
void doSomething(int i1, int i2, int i3 = 0);
 +
</code>
 +
|-
 +
| <code cppqt>
 +
void doSomething(int i1);
 +
</code>
 +
| <code cppqt>
 +
void doSomething(const int i1); // breaks with MSVC and Sun CC
 +
</code>
 +
|-
 +
| <code cppqt>
 +
void doSomething(char *ptr);
 +
</code>
 +
| <code cppqt>
 +
void doSomething(const char *ptr);
 
</code>
 
</code>
 
|}
 
|}

Revision as of 09:09, 15 July 2009

This page is meant as examples of things you cannot do in C++ when maintaining binary compatibility.

Contents

Unexport or remove a class

Before After

class KDECORE_EXPORT KUrl {

  // [...]

};

class KUrl {

  // [...]

};

Change the class hierarchy

Before After

class MyClass: public BaseClass {

  // [...]

};

class MyClass: public BaseClass, public OtherBaseClass {

  // [...]

};

class MyClass: public BaseClass1, public BaseClass2 {

  // [...]

};

class MyClass: public BaseClass2, public BaseClass1 {

  // [...]

};

Change the template arguments of a template class

Before After

template<typename T1> class MyTemplateClass {

   // [...]

};

template<typename T1, typename T2 = void> class MyTemplateClass {

   // [...]

};

Unexport a function

Before After

Q_CORE_EXPORT const char *qVersion();

const char *qVersion();

namespace KSocketFactory {

   KDECORE_EXPORT QTcpSocket *connectToHost(...);

}

namespace KSocketFactory {

   QTcpSocket *connectToHost(...);

}

Inline a function

Before After

int square(int n);

inline int square(int n) { return n * n; }

int square(int n) { return n * n; }

inline int square(int n) { return n * n; }

class Math {

   int square(int n);

};

// the following could be in a .cpp int Math::square(int n) { return n * n; }

class Math {

   int square(int n);

};

// the following could be in a .cpp inline int Math::square(int n) { return n * n; }

class Math {

   int square(int n);

};

// the following could be in a .cpp int Math::square(int n) { return n * n; }

class Math {

   int square(int n)
   { return n * n; }

};

Change the parameters of a function

Before After

void doSomething(int i1, int i2);

void doSomething(int i1, short i2);

void doSomething(int i1, int i2);

void doSomething(int i1, int i2, int i3 = 0);

void doSomething(int i1);

void doSomething(const int i1); // breaks with MSVC and Sun CC

void doSomething(char *ptr);

void doSomething(const char *ptr);

Change the return type

Before After

QTcpSocket *createDevice();

QIODevice *createDevice();

Change the access rights

Before After

class MyClass { protected:

   void doSomething();

};

class MyClass { public:

   void doSomething();

};

Change the CV-qualifiers

Before After

class MyClass { public:

   int something() const;

};

class MyClass { public:

   int something();

};

Add a virtual member function to a class without any

Before After

struct Data {

   int i;

};

struct Data {

   int i;
   virtual int j();

};

Add new virtuals to a non-leaf class

Before After

class MyClass { public:

   virtual ~MyClass();
   virtual void foo();

};

class MyClass { public:

   virtual ~MyClass();
   virtual void foo();
   virtual void bar();

};

Change the order of the declaration of virtual functions

Before After

class MyClass { public:

   virtual ~MyClass();
   virtual void foo();
   virtual void bar();

};

class MyClass { public:

   virtual ~MyClass();
   virtual void bar();
   virtual void foo();

};

Override a virtual that doesn't come from a primary base

class PrimaryBase { public:

   virtual ~PrimaryBase();
   virtual void foo();

};

class SecondaryBase { public:

   virtual ~PrimaryBase();
   virtual void bar();

};

Before After

class MyClass: public PrimaryBase, public SecondaryBase { public:

   ~MyClass();
   void foo();

};

class MyClass: public PrimaryBase, public SecondaryBase { public:

   ~MyClass();
   void foo();
   void bar();

};

Override a virtual with a covariant return with different top address

struct Data1 { int i; }; class BaseClass { public:

   virtual Data1 *get();

};

struct Data0 { int i; }; struct Complex1: Data0, Data1 { }; struct Complex2: virtual Data1 { };

Before After

class MyClass: public BaseClass { public: };

class MyClass: public BaseClass { public:

   Complex1 *get();

};

class MyClass: public BaseClass { public: };

class MyClass: public BaseClass { public:

   Complex2 *get();

};


KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V.Legal