Can someone explain what is the purpose of the & in the print function and how it works. I tried running this with and without the & and got the same thing
#include <iostream>
struct DateStruct
{
int year;
int month;
int day;
};
void print(DateStruct &date)
{
std::cout << date.year << "/" << date.month << "/" << date.day;
}
int main()
{
DateStruct today{ 2020, 10, 14 }; // use uniform initialization
today.day = 16; // use member selection operator to select a member of the struct
print(today);
return 0;
}
That is a reference, and instead of making a copy of your today object to pass into the print() function it passes the object itself into the function.
Copying complex data types like your struct can be a performance hit that passing by reference (or pointer) avoids.