Here is my updated code using QExplicitlySharedDataPointers:
//MYCLASS.H
class MyClassPrivate;
class MyClass {
public:
MyClass();
MyClass(const MyClass &other);
~MyClass();
private:
QExplicitlySharedDataPointer<MyClassPrivate> d;
};
//MYCLASS.H
class MyClassPrivate;
class MyClass {
public:
MyClass();
MyClass(const MyClass &other);
~MyClass();
private:
QExplicitlySharedDataPointer<MyClassPrivate> d;
};
To copy to clipboard, switch view to plain text mode
//MYCLASS.CPP
#include "myclass.h"
#include <QString>
#include <QMap>
public:
MyClassPrivate() { }
MyClassPrivate
(const MyClassPrivate
&other
) : QSharedData(other
), mystring
(other.
mystring), mymap
(other.
mymap) { } QMap<QString, int> mymap;
};
MyClass::MyClass()
: d(new MyClassPrivate)
{ }
MyClass::MyClass(const MyClass &other)
: d(other.d)
{ }
MyClass::~MyClass()
{ }
//MYCLASS.CPP
#include "myclass.h"
#include <QString>
#include <QMap>
class MyClassPrivate : public QSharedData {
public:
MyClassPrivate() { }
MyClassPrivate(const MyClassPrivate &other) : QSharedData(other), mystring(other.mystring), mymap(other.mymap) { }
QString mystring;
QMap<QString, int> mymap;
};
MyClass::MyClass()
: d(new MyClassPrivate)
{ }
MyClass::MyClass(const MyClass &other)
: d(other.d)
{ }
MyClass::~MyClass()
{ }
To copy to clipboard, switch view to plain text mode
//MYCONTAINERTHINGY.H
class MyContainerThingyPrivate;
class MyContainerThingy {
public:
MyContainerThingy(const MyClass &cls);
MyContainerThingy(const MyContainerThingy &other);
~MyContainerThingy();
private:
QExplicitlySharedDataPointer<MyContainerThingyPrivate> d;
};
//MYCONTAINERTHINGY.H
class MyContainerThingyPrivate;
class MyContainerThingy {
public:
MyContainerThingy(const MyClass &cls);
MyContainerThingy(const MyContainerThingy &other);
~MyContainerThingy();
private:
QExplicitlySharedDataPointer<MyContainerThingyPrivate> d;
};
To copy to clipboard, switch view to plain text mode
//MYCONTAINERTHINGY.CPP
#include "mycontainerthingy.h"
#include "myclass.h"
public:
MyContainerThingyPrivate() { }
MyContainerThingyPrivate
(const MyContainerThingyPrivate
&other
) : QSharedData(other
), myclass
(other.
myclass) { } MyClass myclass;
};
MyContainerThingy::MyContainerThingy(const MyClass &cls)
: d(new MyContainerThingyPrivate)
{
d->myclass = cls;
}
MyContainerThingy::MyContainerThingy(const MyContainerThingy &other)
: d(other.d)
{ }
MyContainerThingy::~MyContainerThingy()
{
}
//MYCONTAINERTHINGY.CPP
#include "mycontainerthingy.h"
#include "myclass.h"
class MyContainerThingyPrivate : public QSharedData {
public:
MyContainerThingyPrivate() { }
MyContainerThingyPrivate(const MyContainerThingyPrivate &other) : QSharedData(other), myclass(other.myclass) { }
MyClass myclass;
};
MyContainerThingy::MyContainerThingy(const MyClass &cls)
: d(new MyContainerThingyPrivate)
{
d->myclass = cls;
}
MyContainerThingy::MyContainerThingy(const MyContainerThingy &other)
: d(other.d)
{ }
MyContainerThingy::~MyContainerThingy()
{
}
To copy to clipboard, switch view to plain text mode
I'm getting a compile time error:
compiling myclass.cpp (g++)
compiling mycontainerthingy.cpp (g++)
myclass.h:27: instantiated from here
/usr/include/qt4/QtCore/qshareddata.h:169: error: invalid use of incomplete type 'struct MyClassPrivate'
myclass.h:25: error: forward declaration of 'struct MyClassPrivate'
/usr/include/qt4/QtCore/qshareddata.h:170: error: invalid use of incomplete type 'struct MyClassPrivate'
myclass.h:25: error: forward declaration of 'struct MyClassPrivate'
/usr/include/qt4/QtCore/qshareddata.h:171: warning: possible problem detected in invocation of delete operator:
/usr/include/qt4/QtCore/qshareddata.h:171: warning: invalid use of incomplete type 'struct MyClassPrivate'
myclass.h:25: warning: forward declaration of 'struct MyClassPrivate'
/usr/include/qt4/QtCore/qshareddata.h:171: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined.
*** Exited with status: 2 ***
compiling myclass.cpp (g++)
compiling mycontainerthingy.cpp (g++)
myclass.h:27: instantiated from here
/usr/include/qt4/QtCore/qshareddata.h:169: error: invalid use of incomplete type 'struct MyClassPrivate'
myclass.h:25: error: forward declaration of 'struct MyClassPrivate'
/usr/include/qt4/QtCore/qshareddata.h:170: error: invalid use of incomplete type 'struct MyClassPrivate'
myclass.h:25: error: forward declaration of 'struct MyClassPrivate'
/usr/include/qt4/QtCore/qshareddata.h:171: warning: possible problem detected in invocation of delete operator:
/usr/include/qt4/QtCore/qshareddata.h:171: warning: invalid use of incomplete type 'struct MyClassPrivate'
myclass.h:25: warning: forward declaration of 'struct MyClassPrivate'
/usr/include/qt4/QtCore/qshareddata.h:171: note: neither the destructor nor the class-specific operator delete will be called, even if they are declared when the class is defined.
*** Exited with status: 2 ***
To copy to clipboard, switch view to plain text mode
I traced that back to when I call:
d->myclass = cls;
d->myclass = cls;
To copy to clipboard, switch view to plain text mode
in my MyContainerThingy constructor;
Any ideas?
Bookmarks