Results 1 to 4 of 4

Thread: QExplicitlySharedDataPointer

  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

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QExplicitlySharedDataPointer

    Don't add another "d" member in the subclass of the public component. The original "d" should either be in protected section or the base class of the public component should expose a protected constructor accepting a CRootData pointer and setting it on "d".
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2009
    Posts
    11
    Thanks
    5

    Default Re: QExplicitlySharedDataPointer

    I removed "d" member in CTestPoint and made "d" member in CRoot protected. Now, there is only one set of m_id and m_name members. However, I have to change the set/get functions of CTestPoint a little bit. For example, "void setX(double x) {d->m_x = x;}" has be changed to "void setX(double x) {((QExplicitlySharedDataPointer<CTestpointData>)d)->m_x = x;}". Although this is not so convenient, it is ok.


    Given the existing classes above, now I need to generate a new "CBoard" subclass of CRoot and its corresponding private data class "CBoardData". This CBoard will contain an array of CTestPoint.

    Qt Code:
    1. class CBoard : public CRoot
    2. {
    3. public:
    4. CBoard();
    5. CBoard(const CBoard& other);
    6. ~CBoard();
    7.  
    8. void addTestPoint(CTestPoint tp);
    9. void removeTestPoint(CTestPoint tp);
    10. CTestPoint getTestPoint(int index);
    11. };
    12.  
    13. class CBoardData : public CRootData
    14. {
    15. public:
    16. CBoardData();
    17. CBoardData(const CBoardData &other);
    18. ~CBoardData();
    19.  
    20. QList<CTestPoint> m_testPoints;
    21. };
    To copy to clipboard, switch view to plain text mode 


    There is a problem in the declaration of private data class "CBoardData": it uses CTestPoint, which is a public component class. This introduces a dual-direction reference between public component classes and private data classes, which is not a good design. How to avoid this dual reference while keeping all the public component classes explicitly shared?
    Last edited by wysota; 27th May 2010 at 21:49. Reason: missing [code] tags

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,368
    Thanks
    3
    Thanked 5,017 Times in 4,793 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QExplicitlySharedDataPointer

    You should be able to forward declare the private components in the public header and include the public header in the private one. As far as I remember the docs mention some conditions that need to be filled to be able to do that.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


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.