Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing a vector by reference to function, but change doesn't persist

Tags:

I'm new to C++ so please forgive the noob question. In my program, I pass a vector of "Employee" objects to a function by reference, which calls an Employee member function on each object in the vector, to raise "salary" by "r" percent (in this case 2 percent). I've verified that this is happening by logging the call (the salary is changed within the function), but the changes don't persist...when I print the Employee objects again, the salaries are 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
}