Results 1 to 8 of 8

Thread: What's Up With "const QObject &"

  1. #1
    Join Date
    Jan 2009
    Location
    Camp Lejeune, NC
    Posts
    24
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default What's Up With "const QObject &"

    I see "const QObject &" a lot. It's in both function parameters and in return types. If you want to pass something that can't be modified, why not just pass a "QObject" to it? Does passing and returning by reference save RAM?
    Lenovo ThinkPad T400 - Gentoo Linux 2008.0 Hardened
    Apple iBook G3 - Gentoo Linux 2008.0 Desktop
    Linksys WRT54GL - OpenWrt Kamikaze
    Linksys WRT310N - Stock Crap

  2. #2
    Join Date
    Mar 2007
    Location
    Germany
    Posts
    229
    Thanks
    2
    Thanked 29 Times in 28 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: What's Up With "const QObject &"

    Yes, the calling "by reference" saves RAM if the size of the object is greater than the size of a pointer because no copy of the object has to be created, only the pointer.
    And "call by reference" saves time because no copy of the object has to be created (calling copy constructor of the object and of members of the object). This can be important when a method is called very often.

  3. #3
    Join Date
    Jan 2009
    Location
    Camp Lejeune, NC
    Posts
    24
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What's Up With "const QObject &"

    So they use references to save RAM and constants to keep the referenced data from being modified?
    Lenovo ThinkPad T400 - Gentoo Linux 2008.0 Hardened
    Apple iBook G3 - Gentoo Linux 2008.0 Desktop
    Linksys WRT54GL - OpenWrt Kamikaze
    Linksys WRT310N - Stock Crap

  4. #4
    Join Date
    Sep 2009
    Location
    Belgrade, Serbia
    Posts
    40
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What's Up With "const QObject &"

    It has been a while since I read function calls in C++ so everything that I write might not be 100% accurate but you will get the general idea.

    In C++ parameters are passed to functions by value. What this means is that when you have a code like this

    Qt Code:
    1. void someF(int someCount); // function declaration body is defined somewhere else in the code
    To copy to clipboard, switch view to plain text mode 

    And you make a call to someF by

    Qt Code:
    1. someF(someNumber);
    To copy to clipboard, switch view to plain text mode 

    What basically happens is that someNumber is put on the stack function someF is called and then someCount is initialized by copying the value from the stack. This is both time and memory consuming. Second any change on someCount won't be transferred to someNumber.

    Using reference as a function parameter is similar to using a pointer but less error prone. You only pass a object address to the function which is faster and uses less memory. Only the address of the object is put on stack and copied. Also changes are transferred to the original object that you used to initializes the function parameter.

    Now if someF is declared as follow

    Qt Code:
    1. void someF(int &someCount); // function declaration body is defined somewhere else in the code
    To copy to clipboard, switch view to plain text mode 

    You can use only int type to initialze the parameter. If you have const int that object can't be used because theoretically inside the function you can change the value of the object you passed as the parameter. In functions that don't change the value of the parameter being passed to them it is wise to use const reference as a parameter because you can pass both const and non const objects and if somewhere you try to change the value of the passed parameter it will generate a compiler time error.

    To conclude. Const reference is not a must but it is wise programing practice. You should try to understand and accept this coding practicies because they help you write better code, and make debugging easier.

    I hope I clarified the problem for you. If I wasn't clear enough, my english is not that great, tell me and I will try to explain better.

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

    wswartzendruber (4th October 2009)

  6. #5
    Join Date
    Jan 2008
    Location
    Poland
    Posts
    687
    Thanks
    4
    Thanked 140 Times in 132 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: What's Up With "const QObject &"

    I can just add that without any further explanations you can simply treat const reference arguments as the fastest way to pass arguments to functions. Passing by value is really slow (and expensive in terms of memory usage) because you have to copy whole object.
    I would like to be a "Guru"

    Useful hints (try them before asking):
    1. Use Qt Assistant
    2. Search the forum

    If you haven't found solution yet then create new topic with smart question.

  7. #6
    Join Date
    Jan 2009
    Location
    Camp Lejeune, NC
    Posts
    24
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: What's Up With "const QObject &"

    Much thanks for the explanation.
    Lenovo ThinkPad T400 - Gentoo Linux 2008.0 Hardened
    Apple iBook G3 - Gentoo Linux 2008.0 Desktop
    Linksys WRT54GL - OpenWrt Kamikaze
    Linksys WRT310N - Stock Crap

  8. #7
    Join Date
    Jun 2009
    Posts
    33
    Thanks
    5
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: What's Up With "const QObject &"

    The problem with QObject, along with the size issue, is that the copy constructor has been disabled so passing by value will not work. See http://qt.nokia.com/developer/faqs/582 and http://www.qtcentre.org/forum/f-newb...ors-14812.html


    have fun coding,

    JW
    sw developer

  9. #8
    Join Date
    Jan 2006
    Location
    Frankfurt
    Posts
    500
    Thanks
    1
    Thanked 52 Times in 52 Posts
    Platforms
    MacOS X Unix/X11

    Default Re: What's Up With "const QObject &"

    Where do you see a const QObject& ? Maybe you are referring to a QString which is not a QObject? Because passing a QObject as const & does not make sense.
    It's nice to be important but it's more important to be nice.

Similar Threads

  1. QGraphicsItem doesn't inherit QObject?
    By xyzt in forum Qt Programming
    Replies: 6
    Last Post: 26th September 2011, 14:59
  2. py2app-deployed PyQt app -- throws QObject threading error
    By tory108 in forum Installation and Deployment
    Replies: 4
    Last Post: 20th January 2009, 20:16
  3. QObject and copy Constructors
    By December in forum Qt Programming
    Replies: 5
    Last Post: 17th July 2008, 16:14
  4. Replies: 1
    Last Post: 31st October 2007, 14:14
  5. Reparenting a QObject
    By ghorwin in forum Qt Programming
    Replies: 1
    Last Post: 13th April 2007, 17:21

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.