![pass by reference c++ pass by reference c++](https://i.stack.imgur.com/TVvZs.jpg)
On top of that, you can add const-ness to the type of the argument, enhancing the semantics. In C++ there are technically speaking only two conventions: pass-by-value and pass-by-reference, with some literature including a third pass-by-pointer convention (that is actually pass-by-value of a pointer type). There are some differences in calling conventions in C++ and Java. There's more to this, but these few beginner's rules will get you quite far. (here, "pass by value" is called "pass by copy", because passing by value always creates a copy in C++03)
![pass by reference c++ pass by reference c++](https://www.certiology.com/wp-content/uploads/2016/04/Constant-Reference.gif)
they are to be changed inside the function and such changes should not be reflected outside, in which case you can pass by copy (an alternative would be to pass according to the previous rules and make a copy inside of the function).they are of built-in types, which can be passed by copy.the function should be callable without any argument, in which case you pass by pointer, so that users can pass NULL/ 0/ nullptr instead apply the previous rule to determine whether you should pass by a pointer to a const argument.they are to be changed inside the function and such changes should be reflected outside, in which case you pass by non- const reference.Pass arguments by const reference, except when Optional parameters are best expressed as a std::optional ( boost::optional for older std libs), and aliasing is done fine by reference.Ĭ++11's move semantics make passing and returning by value much more attractive even for complex objects. Passing by pointer is virtually never advised. (Use the previous rules to determine whether to pass by const reference or not.) you pass objects of derived classes as base classes, in which case you need to pass by reference.
![pass by reference c++ pass by reference c++](https://slideplayer.com/slide/12352833/73/images/3/Reference+Parameters+pass-by-value+in+C+example%3A.jpg)
you must mutate the object, in which case, use pass by a non- const lvalue reference,.you do not need ownership of the object and a simple alias will do, in which case you pass by const reference,.