Results 1 to 5 of 5

Thread: Implicit vs. explicit

  1. #1
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Implicit vs. explicit

    Hi,

    Is there an QVector replacement for explicit sharing?

    Currently I am using QVector<float>, but it calls detach() when data() is called. If there a way not to call detach()? In another word, I need explicit sharing QVector.

    Thanks

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

    Default Re: Implicit vs. explicit

    There is no such replacement. You can only pass references to the vector instead of copies of it everywhere.
    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
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Implicit vs. explicit

    Quote Originally Posted by wysota View Post
    There is no such replacement. You can only pass references to the vector instead of copies of it everywhere.
    I have to pass value of QVector<float>, at the same time I want to change value in one vector and all others get the same change.

    Currently I do like this:

    const float* data = vector.constData(); // <- doesn't call detach()
    ((float*)data)[ 3 ] = 5.6; // <- hard cast...

    I know this is a very bad idea, but it is the only way not to call detach() and prevent copy on the write...

    Any better idea?

  4. #4
    Join Date
    Sep 2009
    Posts
    140
    Thanks
    4
    Thanked 17 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Implicit vs. explicit

    Why not using shared pointers, from boost or some other lib?

  5. #5
    Join Date
    Dec 2006
    Posts
    426
    Thanks
    8
    Thanked 18 Times in 17 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Implicit vs. explicit

    I end up creating my own shared array using QVector

    Qt Code:
    1. template <typename Type>
    2. class QfwSharedArray
    3. {
    4. public:
    5.  
    6. QfwSharedArray() : d( new QfwSharedArrayData ) {
    7. }
    8.  
    9. QfwSharedArray( const QVector<Type>& array ) : d( new QfwSharedArrayData ) {
    10. setArray( array );
    11. }
    12.  
    13. QfwSharedArray( const QfwSharedArray& org ) : d( org.d ) {
    14. }
    15.  
    16. ~QfwSharedArray() {
    17. }
    18.  
    19. QfwSharedArray& operator=( const QfwSharedArray& rhs ) {
    20. d = rhs.d;
    21. return *this;
    22. }
    23.  
    24. bool operator==( const QfwSharedArray& rhs ) const {
    25. return d == rhs.d;
    26. }
    27.  
    28. bool operator!=( const QfwSharedArray& rhs ) const {
    29. return !operator==( rhs );
    30. }
    31.  
    32. const QVector<Type>& array() const {
    33. return d->array;
    34. }
    35. QVector<Type>& array() {
    36. return d->array;
    37. }
    38.  
    39. int size() const {
    40. return d->array.size();
    41. }
    42.  
    43. void resize( int size ) {
    44. d->array.resize( size );
    45. }
    46.  
    47. void setArray( const QVector<Type>& array ) {
    48. d->array = array;
    49. }
    50.  
    51. operator QVector<Type>&() {
    52. return d->array;
    53. }
    54.  
    55. operator const QVector<Type>&() const {
    56. return d->array;
    57. }
    58.  
    59. QVector<Type>& operator() () {
    60. return d->array;
    61. }
    62.  
    63. const QVector<Type>& operator() () const {
    64. return d->array;
    65. }
    66.  
    67. Type &operator[]( int i ) {
    68. return d->array[ i ];
    69. }
    70.  
    71. const Type &operator[]( int i ) const {
    72. return d->array[ i ];
    73. }
    74.  
    75. private:
    76.  
    77. class QfwSharedArrayData : public QSharedData {
    78. public:
    79. QVector<Type> array;
    80. };
    81.  
    82. QExplicitlySharedDataPointer<QfwSharedArrayData> d;
    83.  
    84. };
    85.  
    86. template <typename Type>
    87. QDebug operator<<( QDebug dbg, const QfwSharedArray<Type>& sa ) {
    88. dbg.nospace() << sa.array();
    89. return dbg.nospace();
    90. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Implicit sharing vs. c++ refereces
    By IrYoKu in forum Qt Programming
    Replies: 12
    Last Post: 9th November 2011, 23:20
  2. QVector::data() and implicit sharing
    By abernat in forum Qt Programming
    Replies: 2
    Last Post: 7th July 2009, 18:34
  3. Question about implicit sharing
    By Cruz in forum Qt Programming
    Replies: 4
    Last Post: 17th February 2009, 17:03
  4. explicit cast building own math library
    By mickey in forum General Programming
    Replies: 6
    Last Post: 7th February 2008, 05:48
  5. QSharedData - implicit sharing
    By gyre in forum Newbie
    Replies: 4
    Last Post: 28th October 2007, 19:09

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.