Meaning of "&" operator in this context?

Hello, I've bought the book, "Professional C++" as a way to teach myself more about the language and I keep seeing the & operator used. I assumed that they were being used to pass functions by reference but I see no pointers to make that true. What do they do in this context?
// this is a .h file, the code in question is on line 21,22,24,25. I'll also //include the CPP file if that helps.
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#pragma once
#include <string>


namespace Records{
const int kDefaultStartingSalary = 30000;


class Employee
{
public:
Employee();
void promote(int raiseAmount = 1000);
void demote(int demeritAmount = 1000);

void hire();
void fire();
void display() const;
void setFirstName(const std::string& firstName);
const std::string& getFirstName() const;

void setLastName(const std::string& lastName);
const std::string& getLastName() const;

void setEmployeeNumber(int employeeNumber);
int getEmployeeNumber() const;

void setSalary(int newSalary);
int getSalary() const;

bool getIsHired() const;


protected:

private:
std::string mFirstName;
std::string mLastName;
int mEmployeeNumber;
int mSalary;
bool mHired;
};
}

#endif // EMPLOYEE_H

// CPP file
#include "Employee.h"
#include <iostream>
using namespace std;
namespace Records{
Employee::Employee()
:mFirstName("")
,mLastName("")
,mEmployeeNumber(-1)
,mSalary(kDefaultStartingSalary)
,mHired(false)
{
}

void Employee::promote(int raiseAmount)
{
setSalary(getSalary() + raiseAmount);
}
void Employee::demote(int demeritAmount)
{
setSalary(getSalary() - demeritAmount);
}

void Employee::hire(){
mHired = true;
}

void Employee::fire(){
mHired = false;
}

void Employee::display() const{
cout << "Employee: " << getLastName() << ", " << getFirstName() << endl;
cout << "-----------------" << endl;
cout << (mHired ? "Current Employee" : "Former Employee") << endl;
cout << "Employee Number: " << getEmployeeNumber() << endl;
cout << "Salary: $" << getSalary() << endl;
cout << endl;
}



void Employee::setLastName(const string& lastName){
mLastName = lastName;
}

const string& Employee::getLastName() const{
return mLastName;
}


void Employee::setFirstName(const string& firstName){
mFirstName = firstName;
}
const string& Employee::getFirstName() const{
return mFirstName;
}





void Employee::setEmployeeNumber(int employeeNumber){
mEmployeeNumber = employeeNumber;
}
int Employee::getEmployeeNumber()const{
return mEmployeeNumber;
}


void Employee::setSalary(int newSalary){
mSalary = newSalary;
}

int Employee::getSalary() const{
return mSalary;
}

bool Employee::getIsHired() const {
return mHired;
}


}
Last edited on
When you pass a const reference ie setFirstName(const std::string& firstName), you are not passing by value, so you don't create a copy of the parameter passed.
First, please post code with code tags. See http://www.cplusplus.com/articles/jEywvCM9/

1
2
3
4
5
6
7
class Employee
{
  // ...
  void setFirstName(const std::string& firstName);
  const std::string& getFirstName() const;
  // ...
};

The & in these is not an operator. It is part of type definition. The function argument and return value are indeed references due to the &. However, a reference has nothing to do with pointers.
Topic archived. No new replies allowed.