+ 10
Why we pass objects by reference in a function?
3 odpowiedzi
+ 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.
+ 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.
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☺.