I am using QExplicitlySharedDataPointer to create some explicitly shared data classes. I have four classes created as shown below:
CRootData inherits from QSharedData
CTestpointData inherits from CRootData
CRoot has an explicitly shared data pointer to CRootData
CTestPoint inherits from CRoot and has an explicitly shared data pointer to CTestpointData
When I create an instance of CTestPoint, I found that it has two sets of data members of m_id and m_name. This is not what I want. I only want one set of data referred by its d-pointer. How could I achieve this effect? Thanks.
-----------------------------------------------------------------------------------------------------------------
#include <QSharedData>
#include <QString>
{
public:
CRootData() : m_id(0) {m_name.clear();}
CRootData(const CRootData &other)
: QSharedData(other
), m_id
(other.
m_id), m_name
(other.
m_name) {} virtual ~CRootData() {}
int m_id;
};
class CTestpointData : public CRootData
{
public:
CTestpointData() : CRootData() {m_x=0; m_y=0;}
CTestpointData(const CTestpointData &other)
: CRootData(other), m_x(other.m_x), m_y(other.m_y) {}
~CTestpointData() {}
double m_x;
double m_y;
};
class CRoot
{
public:
CRoot() {d = new CRootData();}
CRoot(CRoot& other) : d(other.d) {}
CRoot
(int id,
QString name
) {d
= new CRootData
(); setID
(id
); setName
(name
);
}
virtual ~CRoot() {}
void setID(int id) {d->m_id = id;}
void setName
(QString name
) {d
->m_name
= name;
} int getID() const {return d->m_id;}
QString getName
() const {return d
->m_name;
}
private:
QExplicitlySharedDataPointer<CRootData> d;
};
class CTestPoint : public CRoot
{
public:
CTestPoint() {d = new CTestpointData();}
CTestPoint(CTestPoint& other) : d(other.d) {}
~CTestPoint() {}
void setX(double x) {d->m_x = x;}
void setY(double y) {d->m_y = y;}
double getX() const {return d->m_x;}
double getY() const {return d->m_y;}
private:
QExplicitlySharedDataPointer<CTestpointData> d;
};
#include <QSharedData>
#include <QString>
class CRootData : public QSharedData
{
public:
CRootData() : m_id(0) {m_name.clear();}
CRootData(const CRootData &other)
: QSharedData(other), m_id(other.m_id), m_name(other.m_name) {}
virtual ~CRootData() {}
int m_id;
QString m_name;
};
class CTestpointData : public CRootData
{
public:
CTestpointData() : CRootData() {m_x=0; m_y=0;}
CTestpointData(const CTestpointData &other)
: CRootData(other), m_x(other.m_x), m_y(other.m_y) {}
~CTestpointData() {}
double m_x;
double m_y;
};
class CRoot
{
public:
CRoot() {d = new CRootData();}
CRoot(CRoot& other) : d(other.d) {}
CRoot(int id, QString name) {d = new CRootData(); setID(id); setName(name);}
virtual ~CRoot() {}
void setID(int id) {d->m_id = id;}
void setName(QString name) {d->m_name = name;}
int getID() const {return d->m_id;}
QString getName() const {return d->m_name;}
private:
QExplicitlySharedDataPointer<CRootData> d;
};
class CTestPoint : public CRoot
{
public:
CTestPoint() {d = new CTestpointData();}
CTestPoint(CTestPoint& other) : d(other.d) {}
~CTestPoint() {}
void setX(double x) {d->m_x = x;}
void setY(double y) {d->m_y = y;}
double getX() const {return d->m_x;}
double getY() const {return d->m_y;}
private:
QExplicitlySharedDataPointer<CTestpointData> d;
};
To copy to clipboard, switch view to plain text mode
Bookmarks