Results 1 to 4 of 4

Thread: QExplicitlySharedDataPointer

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2009
    Posts
    11
    Thanks
    5

    Default QExplicitlySharedDataPointer

    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.

    -----------------------------------------------------------------------------------------------------------------
    Qt Code:
    1. #include <QSharedData>
    2. #include <QString>
    3.  
    4. class CRootData : public QSharedData
    5. {
    6. public:
    7. CRootData() : m_id(0) {m_name.clear();}
    8. CRootData(const CRootData &other)
    9. : QSharedData(other), m_id(other.m_id), m_name(other.m_name) {}
    10. virtual ~CRootData() {}
    11.  
    12. int m_id;
    13. QString m_name;
    14. };
    15.  
    16. class CTestpointData : public CRootData
    17. {
    18. public:
    19. CTestpointData() : CRootData() {m_x=0; m_y=0;}
    20. CTestpointData(const CTestpointData &other)
    21. : CRootData(other), m_x(other.m_x), m_y(other.m_y) {}
    22. ~CTestpointData() {}
    23.  
    24. double m_x;
    25. double m_y;
    26. };
    27.  
    28. class CRoot
    29. {
    30. public:
    31. CRoot() {d = new CRootData();}
    32. CRoot(CRoot& other) : d(other.d) {}
    33. CRoot(int id, QString name) {d = new CRootData(); setID(id); setName(name);}
    34.  
    35. virtual ~CRoot() {}
    36.  
    37. void setID(int id) {d->m_id = id;}
    38. void setName(QString name) {d->m_name = name;}
    39. int getID() const {return d->m_id;}
    40. QString getName() const {return d->m_name;}
    41.  
    42. private:
    43. QExplicitlySharedDataPointer<CRootData> d;
    44. };
    45.  
    46. class CTestPoint : public CRoot
    47. {
    48. public:
    49. CTestPoint() {d = new CTestpointData();}
    50. CTestPoint(CTestPoint& other) : d(other.d) {}
    51. ~CTestPoint() {}
    52.  
    53. void setX(double x) {d->m_x = x;}
    54. void setY(double y) {d->m_y = y;}
    55. double getX() const {return d->m_x;}
    56. double getY() const {return d->m_y;}
    57.  
    58. private:
    59. QExplicitlySharedDataPointer<CTestpointData> d;
    60. };
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 26th May 2010 at 23:21. Reason: missing [code] tags

Similar Threads

  1. Inheritance and QExplicitlySharedDataPointer
    By tbcpp in forum Qt Programming
    Replies: 2
    Last Post: 28th May 2010, 08:18
  2. QExplicitlySharedDataPointer
    By qtuser20 in forum Qt Programming
    Replies: 1
    Last Post: 15th September 2009, 07:05

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.