I am new to C ++, so please excuse the noob question. In my program, I pass the Employee object vector to a function by reference, which calls the Employee member function for each object in the vector to increase the "salary" by "r" percent (in this case, 2 percent). I checked that this happens by registering a call (the salary changes inside the function), but the changes are not saved ... when I print Employee objects again, the salaries remain unchanged. Appreciate any help!
// main.cpp void raiseAllBy2Percent(vector<Employee> &v) { for (int i = 0; i < v.size(); i++) { Employee e = v[i]; e.salaryRaise(2); } } // Employee.cpp void Employee::salaryRaise(double r) { cout << "**salaryRaise called"; _salary += (r/100 * _salary); cout << "**new salary: " << _salary; // This logs with the change as expected }
c ++ pass-by-reference vector
Reid belton
source share