Results 1 to 5 of 5

Thread: How to properly return reference to class member?

  1. #1
    Join Date
    Nov 2009
    Posts
    10
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default How to properly return reference to class member?

    Hi, suppose this class:
    Qt Code:
    1. class Foo {
    2.  
    3. protected:
    4. QPoint& bar() const;
    5.  
    6. private:
    7. QPoint m_bar;
    8. };
    9.  
    10. QPoint& Foo::bar() const {
    11. return m_bar;
    12. }
    To copy to clipboard, switch view to plain text mode 

    I get this error:

    error: invalid initialization of reference of type ‘QPoint&’ from expression of type ‘const QPoint’

    However it works if I change it to this:

    Qt Code:
    1. QPoint& Foo::bar() const {
    2. return (QPoint&) m_bar;
    3. }
    To copy to clipboard, switch view to plain text mode 

    1) I don't understand why the compiles says my QPoint is const.
    2) Is it ok to leave the cast there?

  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: How to properly return reference to class member?

    A method can't be const if it returns a non-const reference. Either make that method non-const or make the reference const. This is basic C++, you know... I'm moving the thread to a proper forum.
    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
    Nov 2009
    Posts
    129
    Thanks
    4
    Thanked 29 Times in 29 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to properly return reference to class member?

    1. You declared the member function bar() to be const; that implies that the class and all its data members (excepting any that are declared mutable) are treated as const within that function.

    2. No. You should either return a const reference, return a value instead of a reference, or remove the const specifier from the function.

    Which you choose depends on how you intend the return value to be used. If you don’t have a clear reason for using a reference (const or not), for a small data structure like QPoint you should probably be returning a value.

  4. The following user says thank you to Coises for this useful post:

    agnus (6th December 2009)

  5. #4
    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: How to properly return reference to class member?

    Quote Originally Posted by Coises View Post
    for a small data structure like QPoint you should probably be returning a value.
    Or a const reference.
    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.


  6. #5
    Join Date
    Nov 2009
    Posts
    10
    Thanks
    1
    Thanked 1 Time in 1 Post

    Default Re: How to properly return reference to class member?

    Thank you both!

Similar Threads

  1. Can't compile custom class derived from QGLWidget
    By stealth86 in forum Qt Programming
    Replies: 1
    Last Post: 5th July 2007, 18:55
  2. error undefined reference ...............
    By amit_pansuria in forum Qt Programming
    Replies: 2
    Last Post: 8th June 2007, 15:28
  3. Reference to a class
    By Gayathri in forum Qt Programming
    Replies: 3
    Last Post: 18th December 2006, 15:27
  4. how to correctly compile threads support?
    By srhlefty in forum Installation and Deployment
    Replies: 9
    Last Post: 25th June 2006, 20:15
  5. Strange error while using Q3Canvas
    By Kapil in forum Newbie
    Replies: 13
    Last Post: 15th June 2006, 20:36

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.