1 C++ Reference Parameters Math 130 B Smith: 40 to 45 min. Rate:3. Important discussion on reference parameters! B Smith: Subtle but critical!: Give two reasons a function would use pass by reference? Answer: 1. To provide a called function direct access to the passed variable. 2. To optimize speed of program. With call-by-reference, the copy constructor will not be called with each function call.
17
Embed
1 C++ Reference Parameters Math 130 B Smith: 40 to 45 min. Rate:3. Important discussion on reference parameters! B Smith: 40 to 45 min. Rate:3. Important.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
C++ Reference Parameters
Math 130
B Smith:
40 to 45 min. Rate:3. Important discussion on reference parameters!
B Smith:
40 to 45 min. Rate:3. Important discussion on reference parameters!
B Smith:
Subtle but critical!:
Give two reasons a function would use pass by reference?
Answer:
1. To provide a called function direct access to the passed variable.
2. To optimize speed of program. With call-by-reference, the copy constructor will not be called with each function call.
B Smith:
Subtle but critical!:
Give two reasons a function would use pass by reference?
Answer:
1. To provide a called function direct access to the passed variable.
2. To optimize speed of program. With call-by-reference, the copy constructor will not be called with each function call.
2
Overview
• Reference Parameters
• Common Errors
• Rules of References
• Use of const
• References as Function Arguments
B Smith:
Modify to show learning objects
B Smith:
Modify to show learning objects
3
Reference Parameters
• In function arguments
C and C++ use call-by-value primarily How would you define call-by-reference ?
• In C, call-by-reference requires that we call functions using parameters of data type...? pointer
• C++ introduces another means for passing function arguments via call-by-reference
• This new parameter type in C++ is called a reference parameter
4
Call-By-Reference in C++
void getInput(double& receiver)
{
cout << “enter input number: \n”:
cin >> receiver;
}
The & can also be placed with the parameter name
void getInput(double &receiver);
5
Reference Parameters
• A reference variable (&) is like a pointer that is automatically dereferenced
• Reference variables provide us an alias for a previously declared variable
6
Passing by Value and by Reference#include <iostream>using namespace std; void f(int, int&);
int main(){ int m=22; int n=44; cout << "m= " << m << endl; cout << "n= " << n << endl; f(m,n); cout << "m= " << m << endl; cout << "n= " << n << endl; }
void f(int x, int& y){ x = x+1000; y = y*1000;}
7
swap with pointers //Demonstrates passing by reference #include <iostream> void swap(int *x, int *y); int main() { int x = 5, y = 10; cout << "Main. Before swap, x: " << x << " y: " << y << "\n"; swap(&x, &y); cout << "Main. After swap, x: " << x << " y: " << y << "\n"; return 0; } void swap (int *px, int *py) { int temp; cout << "Swap. Before swap, *px: " << *px << " *py: " << *py << "\n"; temp = *px; *px = *py; *py = temp; cout << "Swap. After swap, *px: " << *px << " *py: " << *py << "\n"; }
8
swap with references //Demonstrates passing by reference // using references! #include <iostream> void swap(int& x, int& y); int main() { int x = 5, y = 10; cout << "Main. Before swap, x: " << x << " y: " << y << "\n"; swap(x,y); cout << "Main. After swap, x: " << x << " y: " << y << "\n"; return 0; } void swap (int& rx, int& ry) { int temp; cout << "Swap. Before swap, rx: " << rx << " ry: " << ry << "\n"; temp = rx; rx = ry; ry = temp; cout << "Swap. After swap, rx: " << rx << " ry: " << ry << "\n"; }
9
What will be the output?
#include <iostream>int main ( ){ float total = 20.5 ; float& sum = total ; cout <<"sum = "<< sum << endl ; sum = 18.6 ; cout << "total = “ << total << endl ; return 0 ;}
declare and initialize total
declare another name for total
this changes the value in total
10
Common Errors with References
• The references should be of the same data type as the variable to which it refers.
• What is the output of the following program segment? (If it even compiles!)
#include <iostream>using namespace std;int main(){ int num = 10; float& numref = num; numref = 23.6; cout <<"The value of num is " << num << endl; <<"The value of numref is "<< numref << endl; return 0;}
this does not equate numref to num
11
Reassignment of References
int main(){ int intOne; int &aRef = intOne;
intOne = 5;
int intTwo = 8; aRef = intTwo; return 0;}
aRef: 8intTwo:
5intOne: 58
The reinitialization of the
reference variable failed!
B Smith:
just discussed in previous slide. This slide is only useful to the extent that it shows that you’re unable to dereference.
B Smith:
just discussed in previous slide. This slide is only useful to the extent that it shows that you’re unable to dereference.
13
Keyword const• To help avoid this type of confusion, C++ allows you to
explictly prevent changing the value of the referenced object
• Using const designates aRef as read-onlyint main(){ int intOne; const int &aRef = intOne;
intOne = 5;
int intTwo = 8; aRef = intTwo; //the compiler will catch return 0;}
• Use const wherever possible in your programs It reduces the likelihood of unintentional modification It communicates to other programmers your intentions
to restrict modification to data members
16
constant call-by-reference parameter
int isLarger(BankAccount account1, BankAccount account2)
//Returns true if the balance in acct1 is gtr than that