references in c 2b 2b

Solutions on MaxInterview for references in c 2b 2b by the best coders in the world

showing results for - "references in c 2b 2b"
Ken
30 Sep 2016
1#include <vector>
2
3void simple_reference_example() {
4    int x; 
5    int &y = x;     // y refers directly to x.
6    x = 10; 
7    std::cout << y; // prints 10 
8}
9
10/* careful! edits to d in this function affect the original */
11void pass_by_ref(std::vector<int> &d) {
12	d[0] = 10;
13}
14
15/* 'const' prevents changing of data in d */
16void pass_by_const_ref(const std::vector<int> &d) { }
17
18int main(){
19	std::vector<int> data(1, 0);   // initialize 1 element vector w/ the value 0
20    pass_by_ref(ints);
21    std::cout << data[0];          // prints 10
22  	pass_by_const_ref(ints);
23}
24