Thursday, March 21, 2019

function - When to use references as parameters in C++











If I have a function that takes in some parameters and then does something with the parameters without needing to change their inherent value, is there any benefit from using pass by reference versus pass by value?



Answer



Yes. Passing by value copies the argument, which might be very expensive (or not even possible). If you want to pass by reference, but not modify the object, pass by const-reference.



As an example of an object that cannot be passed by value:



class Foo {
public:
Foo() {}
private:
Foo(const Foo&); // copy-constructor is not accessible

}

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...