Lecture 2 - Slide 18 : 29
Parameter passing in C++
It is possible to pass parameters by value, by a pointer, and via a C++ reference
Call by value
Copy actual parameters and bind to formal parameters
Formal parameters are
initialized
- not assigned
For user-defined types: It is possible to control what copying means - via a
copy constructor
In the copy constructor itself, call-by-C++-reference parameter passing is crucial
Details
in a forthcoming lecture
In the special case: Passing pointers by value (C-style call-by-reference)
Call by (C++) reference
The formal parameter becomes an alias of the actual parameter
If non-const, you intend to change the value of the actual parameter
Call by const reference
is an attractive alternative to
call by value
parameters
No overhead due to copying
No risk of modifying the actual parameter via the formal parameter
C#: Ref parameters