Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. unique_ptr operator overloading is disabled by QScopedPointer
Forum Updated to NodeBB v4.3 + New Features

unique_ptr operator overloading is disabled by QScopedPointer

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 3 Posters 271 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    DungeonLords
    wrote on last edited by
    #1

    In my example
    QScopedPointer or std::unique_ptr disable operator overloading. Can I use my operator overloading with QScopedPointer or unique_ptr?

        class1 cl1;
        std::unique_ptr<class1> cl2;
        QScopedPointer<class1> cl3;
        QBuffer buffer;
        buffer.open(QIODevice::WriteOnly);
        QDataStream myStream(&buffer);
        myStream << cl1 << "    " << cl2.get() << "    " << cl3.get();
        qDebug() << buffer.data();
    

    output

    "\x00\x00\x00\x0E\x00""d\x00""e\x00""f\x00""a\x00u\x00l\x00t\x00\x00\x00\x05    \x00\x00\x00\x00\x00\x05    \x00\x00"
    

    I make full code example based on topic

    1 Reply Last reply
    0
    • C Offline
      C Offline
      ChrisW67
      wrote on last edited by ChrisW67
      #3

      The posted code will not compile for me (Linux, GCC13, Qt 6.7.2):

      /tmp/unique_ptr_operator_overloading_unique_ptr/src/main.cpp: In function ‘int main(int, char**)’:
      /tmp/unique_ptr_operator_overloading_unique_ptr/src/main.cpp:15:42: error: use of deleted function ‘QDataStream& QDataStream::operator<<(const volatile void*)’
         15 |     myStream << cl1 << "    " << cl2.get() << "    " << cl3.get();
            |                                          ^
      

      uniqueptr::get() and QScopedPointer::get() return pointers and there is no suitable streaming operator according to my compiler.

      Anyway, @VRonin is correct but you need to initialise cl2 and cl3 with valid pointers. Presently, cl2 and cl3 are wrappers on nullptrs, so deferencing these might be terminal.

          class1 cl1;
          std::unique_ptr<class1> cl2 = std::make_unique<class1>();
          QScopedPointer<class1> cl3(new class1);
          QBuffer buffer;
          buffer.open(QIODevice::WriteOnly);
          QDataStream myStream(&buffer);
          myStream << cl1 << "    " << *cl2 << "    " << *cl3;
          qDebug() << buffer.data();
          return a.exec();
      }
      
      1 Reply Last reply
      1
      • VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by VRonin
        #2

        You can just dereference the pointer...
        myStream << cl1 << " " << *cl2 << " " << *cl3;

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        1
        • C Offline
          C Offline
          ChrisW67
          wrote on last edited by ChrisW67
          #3

          The posted code will not compile for me (Linux, GCC13, Qt 6.7.2):

          /tmp/unique_ptr_operator_overloading_unique_ptr/src/main.cpp: In function ‘int main(int, char**)’:
          /tmp/unique_ptr_operator_overloading_unique_ptr/src/main.cpp:15:42: error: use of deleted function ‘QDataStream& QDataStream::operator<<(const volatile void*)’
             15 |     myStream << cl1 << "    " << cl2.get() << "    " << cl3.get();
                |                                          ^
          

          uniqueptr::get() and QScopedPointer::get() return pointers and there is no suitable streaming operator according to my compiler.

          Anyway, @VRonin is correct but you need to initialise cl2 and cl3 with valid pointers. Presently, cl2 and cl3 are wrappers on nullptrs, so deferencing these might be terminal.

              class1 cl1;
              std::unique_ptr<class1> cl2 = std::make_unique<class1>();
              QScopedPointer<class1> cl3(new class1);
              QBuffer buffer;
              buffer.open(QIODevice::WriteOnly);
              QDataStream myStream(&buffer);
              myStream << cl1 << "    " << *cl2 << "    " << *cl3;
              qDebug() << buffer.data();
              return a.exec();
          }
          
          1 Reply Last reply
          1
          • D DungeonLords has marked this topic as solved on
          • D DungeonLords has marked this topic as solved on
          • D Offline
            D Offline
            DungeonLords
            wrote on last edited by DungeonLords
            #4

            Thanks @ChrisW67 @VRonin problem solved! I updated my repo for another beginners...

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved