PDA

View Full Version : What's Up With "const QObject &"



wswartzendruber
3rd October 2009, 13:54
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?

Boron
3rd October 2009, 15:52
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.

wswartzendruber
3rd October 2009, 18:52
So they use references to save RAM and constants to keep the referenced data from being modified?

frenk_castle
4th October 2009, 15:20
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




void someF(int someCount); // function declaration body is defined somewhere else in the code



And you make a call to someF by




someF(someNumber);



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




void someF(int &someCount); // function declaration body is defined somewhere else in the code



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.

faldzip
4th October 2009, 18:07
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.

wswartzendruber
4th October 2009, 19:54
Much thanks for the explanation.

spawn9997
13th October 2009, 20:00
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-newbie-4/t-qobject-and-copy-constructors-14812.html


have fun coding,

JW
sw developer

axeljaeger
15th October 2009, 11:53
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.