Hi All,
I'm trying to figure out how to pass an object to a function by constant reference. If I write a single class, can I write a function in this class that accepts an object of this class by constant reference? If so (or in any case) how does one write a function to accept an object by constant reference? Apologies if this seems muddled, I'm having difficulty getting my head around it and I can't find examples anywhere.
Here's an attempt I made. It's a class Machine which has three stated and I want to write a function which increments the integer state of a Machine object using "pass by constant reference".
#include<iostream>
#include<string>
using namespace std;
class Machine
{
public:
string owner;
string make;
int rackID;
Machine();
virtual void displayMachineDetails();
virtual void increment();
};
void Machine::displayMachineDetails()
{
cout <<"\nMachine owner: " << owner << "\nMake: " << make
<< "\nRack ID: " << rackID << endl;
}
//This is the function to which I want to pass an object by constant reference
void increment(const Machine &aMachine, int amount = 1) {
rackID = rackID - amount;
}
int main()
{
Machine a = Machine("Me", "IBM", 1);
a.increment();
}