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?
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
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.
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
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:
void someF(int someCount); // function declaration body is defined somewhere else in the codeTo copy to clipboard, switch view to plain text mode
And you make a call to someF by
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:
void someF(int &someCount); // function declaration body is defined somewhere else in the codeTo 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.
wswartzendruber (4th October 2009)
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):
- Use Qt Assistant
- Search the forum
If you haven't found solution yet then create new topic with smart question.
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
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
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.
Bookmarks