A C++ reference is an alias bound to an existing object or function. It provides access to that target without storing a separate object value, unlike a value variable. section 6.2.2
Passing an existing object
The appointment tester declares print(const Appointments& appts), using a const reference for an existing Appointments object. The surrounding vector example contrasts this with returning Date objects by value, which can trigger copying and performance surprises. section 6.2.2
Listing 6.15 (Program 6.9 SurpriseVector-3): tester.cpp
#include <iostream>
#include <vector>
#include "Appointments.h"
#include "Date.h"
using namespace std;
void print(const Appointments& appts); #1
int main()
{
cout << "TEST CONSTRUCTOR" << endl;
Date *d1969 = new Date(1969, 7, 20); #2
cout << "d1969: " << *d1969 << endl;
Date *d2025 = new Date(2025, 9, 2); #2
cout << "d2025: " << *d2025 << endl;
Date *d2027 = new Date(2027, 4, 3); #2
cout << "d2027: " << *d2027 << endl;
Appointments appts;
cout << endl << "TEST APPEND" << endl;
cout << "append d2025: " << *d2025 << endl;
appts.append(d2025);
cout << "append d2027: " << *d2027 << endl;
appts.append(d2027);
print(appts);
cout << endl << "TEST INITIALIZATION" << endl;
cout << "initialize d = d2025:" << endl;
Date *d = d2025;
cout << *d << endl;
cout << endl << "TEST ASSIGNMENT" << endl;
cout << "assign d = d2027:" << endl;
d = d2027;
cout << *d << endl;
cout << endl << "TEST INSERTION" << endl;
cout << "insert " << *d1969
<< " at index [0]:" << endl;
appts.insert(0, d1969);
print(appts);
cout << endl << "TEST REMOVAL" << endl;
cout << "remove " << *appts.at(1)
<< " at index [1]:" << endl;
appts.remove(1);
print(appts);
cout << endl << "TEST DESTRUCTOR" << endl;
delete d1969; #3
delete d2025; #3
delete d2027; #3
cout << endl << "Done!" << endl;
return 0;
}
void print(const Appointments& appts)
{
int i = 0;
cout << "printing vector ..." << endl;
for (Date *date : appts.get_dates())
{
cout << "[" << i << "] " << *date << endl;
i++;
}
}
#1 Reference parameter Appointments&
#2 Dynamically create Date objects and initialize the variables with pointers to the objects.
#3 Explicitly delete the dynamically created Date objects.
The tester shows a const reference parameter alongside pointer-based Date storage, making alternative ways to access existing objects visible. section 6.2.2
















