Results 1 to 15 of 15

Thread: How to return a reference of a QPointer ?

  1. #1
    Join Date
    Aug 2010
    Posts
    22
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default How to return a reference of a QPointer ?

    Hi, I need to do something like this:

    Qt Code:
    1. class Apple {
    2. private:
    3. QPointer<Tree> m_tree;
    4. public:
    5. Tree &tree();
    6. }
    7.  
    8. Tree &Apple::tree()
    9. {
    10. return m_tree;
    11. }
    To copy to clipboard, switch view to plain text mode 

    But the compiler says this for the line 'return m_tree':
    error C2440: 'return' : cannot convert from 'QPointer<T>' to 'Tree &'

    How to change the line to make it work?
    Thanks.

  2. #2
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to return a reference of a QPointer ?

    What "work" means, what are you trying to achieve.
    Do you want to return a QPointer<Tree> or a dereferenced QPointer?

  3. #3
    Join Date
    Aug 2010
    Posts
    22
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to return a reference of a QPointer ?

    Thanks, just want to return a reference to m_tree.

  4. #4
    Join Date
    Oct 2010
    Posts
    3
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to return a reference of a QPointer ?

    What about something like :

    return *m_tree;

    According to the documentation here http://doc.qt.nokia.com/4.7/qpointer.html , operator* should return a reference.

  5. The following user says thank you to mpalomas for this useful post:

    jezz (4th February 2011)

  6. #5
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to return a reference of a QPointer ?

    Why not returning the m_tree pointer? Since it's just a pointer not the whole object?
    Always try to keep the code simple.
    Qt Code:
    1. QPointer<Tree>Apple::tree()
    2. {
    3. return m_tree;
    4. }
    To copy to clipboard, switch view to plain text mode 

  7. #6
    Join Date
    Aug 2010
    Posts
    22
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to return a reference of a QPointer ?

    Quote Originally Posted by mpalomas View Post
    What about something like :

    return *m_tree;

    According to the documentation here http://doc.qt.nokia.com/4.7/qpointer.html , operator* should return a reference.
    Thanks, this works.

    Quote Originally Posted by Zlatomir View Post
    Why not returning the m_tree pointer? Since it's just a pointer not the whole object?
    Always try to keep the code simple.
    Qt Code:
    1. QPointer<Tree>Apple::tree()
    2. {
    3. return m_tree;
    4. }
    To copy to clipboard, switch view to plain text mode 
    Thanks, but the usage would look like this
    Qt Code:
    1. QPointer<Tree> blah = apple->tree();
    To copy to clipboard, switch view to plain text mode 

    It is cleaner like this:
    Qt Code:
    1. Tree blah = apple->tree();
    To copy to clipboard, switch view to plain text mode 

  8. #7
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to return a reference of a QPointer ?

    Well, you said you need the pointer when i asked the first question (what you are trying to achieve), i asked that because you were somewhere in the "middle" between the two solutions.

    LE: for that you didn't really needed the reference return. Since you are coping the object you can return the value.

  9. #8
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to return a reference of a QPointer ?

    It is cleaner like this:
    Qt Code:
    1. Tree blah = apple->tree();
    To copy to clipboard, switch view to plain text mode 
    You can always use typedef:
    Qt Code:
    1. typedef QPointer<Tree> TreePtr;
    2. TreePtr p = apple->tree();
    To copy to clipboard, switch view to plain text mode 
    Looks clean enough to me And its just a pointer, so it does not require to copy the whole tree

  10. #9
    Join Date
    Aug 2010
    Posts
    22
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to return a reference of a QPointer ?

    Quote Originally Posted by Zlatomir View Post
    Well, you said you need the pointer when i asked the first question (what you are trying to achieve), i asked that because you were somewhere in the "middle" between the two solutions.

    LE: for that you didn't really needed the reference return. Since you are coping the object you can return the value.
    I mean &m_tree (reference to m_tree), not pointer.


    Quote Originally Posted by stampede View Post
    You can always use typedef:
    Qt Code:
    1. typedef QPointer<Tree> TreePtr;
    2. TreePtr p = apple->tree();
    To copy to clipboard, switch view to plain text mode 
    Looks clean enough to me And its just a pointer, so it does not require to copy the whole tree
    It would need lots of typedef if there are lots of pointers to different classes.

  11. #10
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to return a reference of a QPointer ?

    Quote Originally Posted by jezz View Post
    I mean &m_tree (reference to m_tree), not pointer.
    Q: Now you have a reference to m_tree? A: NO
    You have a copy of a Tree object.

    You use pointers or references to prevent the copy of whole objects when you pass them from one place to another.

  12. The following user says thank you to Zlatomir for this useful post:

    jezz (4th February 2011)

  13. #11
    Join Date
    Sep 2009
    Location
    Wroclaw, Poland
    Posts
    1,394
    Thanked 342 Times in 324 Posts
    Qt products
    Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to return a reference of a QPointer ?

    Qt Code:
    1. Tree blah = apple->tree();
    To copy to clipboard, switch view to plain text mode 
    There is no difference here if tree() returns a reference or value, each time Tree::Tree(const Tree& t) copy constructor is invoked.
    If you want to prevent copying, return a pointer or:
    Qt Code:
    1. Tree& blah = apple->tree();
    To copy to clipboard, switch view to plain text mode 

    It would need lots of typedef if there are lots of pointers to different classes.
    One per class, not that much

  14. The following user says thank you to stampede for this useful post:

    jezz (4th February 2011)

  15. #12
    Join Date
    Aug 2010
    Posts
    22
    Thanks
    11
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to return a reference of a QPointer ?

    My bad, I should have put it this way
    Qt Code:
    1. Tree& blah = apple->tree();
    To copy to clipboard, switch view to plain text mode 

  16. #13
    Join Date
    May 2010
    Location
    Romania
    Posts
    1,021
    Thanks
    62
    Thanked 260 Times in 246 Posts
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: How to return a reference of a QPointer ?

    Just be careful so that you don't end-up with "dangling" references.

    The references can't be NULL (that is why some assume they are safer than pointers) but with the help of pointers they can get invalid...

  17. #14
    Join Date
    Oct 2006
    Location
    New Delhi, India
    Posts
    2,467
    Thanks
    8
    Thanked 334 Times in 317 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to return a reference of a QPointer ?

    It is cleaner like this:
    Qt Code:
    Switch view

    Tree blah = apple->tree();
    Even if it is Tree& blah = apple->tree();
    I would disagree it is cleaner code.
    If you use pointers, you know you are modifying something which you haven't created. But in case of reference it is hard to know.

    Also if references are returned, I would prefer to use it on left hand side. For eg if you have QList<int> numbers;
    Then having a reference with [] operator,,,, I can assign value like - numbers[index] = 10;
    Lets say I wrote code like -
    Qt Code:
    1. int & temp = numbers[index];
    2. ...
    3. ... // assume you have many lines of code here...
    4. temp = 10; // Can you still know you are actually modifying something ? oops ,, did you forget ?
    5. // numbers[index] = 10; // with this line you know you are modifying something..
    To copy to clipboard, switch view to plain text mode 

    Rest depends on personal choice

  18. #15
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: How to return a reference of a QPointer ?

    Uh - what's the point of this exercise? You declare m_tree private, but then you hand over a direct reference to it whenever the user asks. There's no point in making m_tree private in such a case, since you throw away privacy by passing back the object.

    Here's a more sane solution that accomplishes exactly the same thing with far less code: make m_tree public and access it directly via apple.m_tree. There's no difference between that and the far more convoluted constructs you're wrestling with above.

Similar Threads

  1. How to cast QPointer<T> to QPointer<childT>
    By cafu in forum Qt Programming
    Replies: 4
    Last Post: 19th March 2010, 10:51
  2. Problem using QPointer
    By weaver4 in forum Newbie
    Replies: 8
    Last Post: 20th February 2010, 05:05
  3. How to properly return reference to class member?
    By agnus in forum General Programming
    Replies: 4
    Last Post: 6th December 2009, 02:41
  4. QMutableVectorIterator and QPointer?
    By Scorp2us in forum Qt Programming
    Replies: 1
    Last Post: 8th November 2008, 19:39
  5. QPointer and double deletion
    By mtrpoland in forum Qt Programming
    Replies: 6
    Last Post: 28th September 2007, 12:49

Tags for this Thread

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.