Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the role of passing by reference when you do not modify variables?

Are these references(&) just an issue of saving memory or idioms or is there a reason to why statements like these use references when passing by copy would accomplish the same thing.

template <class T>
bool testGreater (const T& one, const T& two);

or an object declaration such as this:

Cars BMW (const Engine&); //where engine is a class

What is the function of passing by reference when you do not need to modify the item passed?

like image 768
Scholar Avatar asked Dec 16 '22 04:12

Scholar


1 Answers

When you pass by value you must make a copy of the object. Depending on what object is used to instantiate the template, this can be expensive. (or impossible. Some objects are not copyable)

like image 189
Dale Wilson Avatar answered Dec 27 '22 10:12

Dale Wilson