Answers for "cpp references"

C++
0

reference variablesr in c++

Value of i : 5
Value of i reference : 5
Value of d : 11.7
Value of d reference : 11.7
Posted by: Guest on December-13-2020
0

references in c++

#include <vector>

void simple_reference_example() {
    int x; 
    int &y = x;     // y refers directly to x.
    x = 10; 
    std::cout << y; // prints 10 
}

/* careful! edits to d in this function affect the original */
void pass_by_ref(std::vector<int> &d) {
	d[0] = 10;
}

/* 'const' prevents changing of data in d */
void pass_by_const_ref(const std::vector<int> &d) { }

int main(){
	std::vector<int> data(1, 0);   // initialize 1 element vector w/ the value 0
    pass_by_ref(ints);
    std::cout << data[0];          // prints 10
  	pass_by_const_ref(ints);
}
Posted by: Guest on July-15-2021

Browse Popular Code Answers by Language