+ 10

Why we pass objects by reference in a function?

22nd Sep 2016, 2:14 PM
Mahadevan S K
3 Respostas
+ 13
Imagine if you had a class that represents a model of a huge building. If you were to pass it to a function by value, you would probably need to copy huge amounts of data, which is not intended or optimal. On the other hand, if you pass it by reference, only the address is sent to the function, which is many times smaller than the object itself.
23rd Sep 2016, 7:51 AM
Zeljko Markovic
+ 3
It needs less memory, bcoz by passing object by reference function works on these objects and does not need to create new "local" objects.
22nd Sep 2016, 2:43 PM
Lunesco
Lunesco - avatar
0
U know that everytime u pass an object by value it is copied by a in build constructor.Now every time u pass by value a copy is created and again that copy passes a object by value.So this continues till the heap memory completely fills up an the program stops.By passing it by reference u just skip the constructor calling part and error does not occur . Hope it helps☺.
7th Nov 2016, 3:03 AM
Oreki Houtaro
Oreki Houtaro - avatar