since local objects get destroyed once the function ends.
so i am confused if it is good/bad to pass local objects by reference.
for example:
Code:void demoFunc() { DemoClass demoObj; passByReference(demoObj); }
since local objects get destroyed once the function ends.
so i am confused if it is good/bad to pass local objects by reference.
for example:
Code:void demoFunc() { DemoClass demoObj; passByReference(demoObj); }
This example is fine because the function demoFunc won't end until the function passByReference returns. Thus it's safe.
However, returning demoObj from the function demoFunc is bad because demoObj will be destroyed before the function that uses the object after demoFunc can use it.
In this case, you either need to return it via a pointer (allocate on heap) or have the function returning a local object take a reference or pointer to a buffer to store the object.
It is perfectly fine to pass local objects by reference since they will still exist in the current scope when control enters the function called. It is bad to return references to local objects since they will not exist when control leaves the function in whose scope they exist.since local objects get destroyed once the function ends.
so i am confused if it is good/bad to pass local objects by reference.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
thanks. but i was thinking what if passByReference() saves the object for a later use?
i am really confused! sorry!
If it saves it for later use, then it's bad.
It's really simple. The functions must never use the object for longer than the function that owns it. If any function tries to use the object after the function that owns it has returned, then the object will be destroyed.
General rules:
Good:
The called function uses the object and then returns.
Bad:
The called function stores the object for later use.
A function tries to use a local object returned by a function.
These apply to references.
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
thanks again. but the real confusion is i don't have any idea if the function will save it for later use or not!
and i am talking from my experience. i was writing Qt code today. and just stuck here. thinking about should i pass by reference or not
If unsure, it's best if you allocate the object on heap and pass via pointer/reference.
It's better to be safe than to rely on undefined behavior.
Otherwise, you can try to step through the code or ask experts if the function really will store the object for later or not (which is doubtful if the object is passed by reference).
You should not need to know, in general, if the called function is taking the data by reference or not. It should be explicit that the object has a reference being held by the function, and such functions should be rare and "special" (such as setup functions, perhaps).
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
suppose the function that is passing the object is not a class member.
in this case to make the object on heap has two options. one is make it global, other is make it local.
now the go out of scope problem is solved. but the memory leak will occur if the object is made local. and other wise we have problems associated with globals.
uuuurgh!! i think too much i think! sorry!
Check the documentation, but generally it is unlikely that the function will save it for later use, especially since its own lifetime is less than the lifetime of your calling function. Such a "saving" of a reference could happen if you were invoking a constructor of a class that has a reference member, because the lifetime of the object could exceed that of your calling function.but the real confusion is i don't have any idea if the function will save it for later use or not!
You cannot make that decision: the Qt developers who designed the interface of that function decided whether you can pass by reference or not.and i am talking from my experience. i was writing Qt code today. and just stuck here. thinking about should i pass by reference or not
EDIT:
Elysia was not suggesting global variables but new (with a smart pointer, for example), if you really wanted to be paranoid. Local objects exist on the stack, not the heap.in this case to make the object on heap has two options. one is make it global, other is make it local.
now the go out of scope problem is solved. but the memory leak will occur if the object is made local. and other wise we have problems associated with globals.
Last edited by laserlight; 03-31-2008 at 07:17 AM.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
laserlight i actually meant this:
Code:DemoClass * demoObj1; void demoFunc() { demoObj1 = new DemoClass; // Option 1 DemoClass * demoObj2 = new DemoClass; // Option 2 }
But as stated, a function that "grabs" an object passed by reference should clearly state that it does so in the documentation of the function.
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Yes, you would need a global var or pass in a pointer that contains the object to pass, perhaps.
But before you start making presumptions, make sure you know the function. Will it store a reference? Read the documentation. It should contain all knowledge you need.
thanks!
this is the case here:
i am 100% sure the QtFunction will have to save the obj somewhere because QtFunction is a constrcutor.Code:QtFunction(QtClass obj) { } // no reference MyFunction(QtClass & ref) { } // i thought i should use reference for efficiency
MyFunction will invoke QtFunction, and that is where i was confused, if i declare it ref or non ref.