Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    QScopedPointer and assignment value

    General and Desktop
    2
    4
    1347
    Loading More Posts
    • 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.
    • J
      jh224 last edited by

      Is there a way to use an assignment operator for the underlying object of a QScopedPointer, or is this verboten?

      e.g.
      @

      int testFunction(bool myFlag)
      {
      QScopedPointer<int> a(new int(0));

      if(myFlag == false)
      

      // I've the tried the following but can't get it to work...I'm probably misreading the
      // documentation, but this seems like it should work
      a.data() = 5; // does not work
      else
      a.data() = 10;

        return a;
      

      }

      @

      1 Reply Last reply Reply Quote 0
      • J
        jh224 last edited by

        I found the solution.

        *a = 5;

        1 Reply Last reply Reply Quote 0
        • K
          koahnig last edited by

          Good to see that you have found the solution yourself.
          Anyway here is an example of use:
          @
          #include <QScopedPointer>
          #include <iostream>

          int foo ()
          {
          QScopedPointer < int > iptr ( new int ( 5 ) );

          *iptr = 10;
          return *iptr;
          

          }

          int main(int argc, char *argv[])
          {
          std:: cout << foo () << std::endl;
          }
          @

          Scoped and shared pointer are designed to be used as simple pointers. However, to explain it simple, they avoid memory leaks and access of already released memory by automatic handling of deletion.

          Vote the answer(s) that helped you to solve your issue(s)

          1 Reply Last reply Reply Quote 0
          • J
            jh224 last edited by

            Thanks for the example.

            1 Reply Last reply Reply Quote 0
            • First post
              Last post