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. QScopedPointer and assignment value

QScopedPointer and assignment value

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 1.6k 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.
  • J Offline
    J Offline
    jh224
    wrote on last edited by
    #1

    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
    0
    • J Offline
      J Offline
      jh224
      wrote on last edited by
      #2

      I found the solution.

      *a = 5;

      1 Reply Last reply
      0
      • K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

        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
        0
        • J Offline
          J Offline
          jh224
          wrote on last edited by
          #4

          Thanks for the example.

          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