I had wanted to find an example to express my understanding of binary compatibility, but blown it. I want to change the layout of members of class in the DLL by add members to class at the beginning or in the middle, and expect that the variable cannot be accessed correctly or accessing the variable will generate crash.However, everything goes well. I find, no matter how I add member variable to any position of class,there are no crash and not breaking binary compatibility. My code as following:
//untitled1_global.h
#include <QtCore/qglobal.h>
#if defined(UNTITLED1_LIBRARY)
# define UNTITLED1_EXPORT Q_DECL_EXPORT
#else
# define UNTITLED1_EXPORT Q_DECL_IMPORT
#endif
//base.h
class UNTITLED1_EXPORT Base
{
public:
Base();
double getA();
double getB();
private:
int arr[100]; //Add later to update the DLL
double a;
double b;
};
//derived.h
#include "dpbase.h"
class UNTITLED1_EXPORT Derived : public Base
{
public:
Derived();
void setC(double d);
double getC();
private:
char arrCh[100]; //Add later to update the DLL
double c;
};
Below is the client code,base.h
、derived.h
included aren't same as in the DLL, one is annotated and one not. Implementation and declaration are separate in the DLL.I tried to access the variable directly and access the variable by funcation(such as annotated at the beginning of main.cpp
).
//main.cpp
#include "dpbase.h"
#include "dpbase2.h"
#include <QDebug>
#include <QApplication>
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
Base base;
qDebug() << base.getA();
qDebug() << base.getB();
Derived base2;
base2.setC(50);
qDebug() << base2.getC();
return a.exec();
}
Among them, class Base
,Derived
is exported from dll.
No matter how I add member variable to whether Base
or Derived
anywhere,there are no crash and not breaking binary compatibility.
I am using qt.There is a same question here, but no help for me.
Furthermore, I delete all member var of class in the DLL, I still use nonexistent variable in the client by linking the DLL,assign value, get it...It seems that there is enough space reserved in the dynamic library to be redefined by the client, even if no member variable is defined.So strange!
My question is, why changing the layout of members of class in the DLL, will not break binary compatibility?And deleting all member var of class in the DLL but why the caller can still use members in the .h file?
IDPBase
that only has pure virtual methods, and export a function that returns instances of this class). – selbie