Hi, all I following lessons in this book. I am learning the concepts taught. Sometimes when I put it down I get rusty. I am back beginning the lessons on operator overloading. I seemed to forget the reason for the use of a reference when referring to an object that is instantiated. I ran a test and added something the constructor would do to show me creation and the result is the same using a reference or not. So could some explain why the instructor uses a reference? Thanks
#include <iostream>
usingnamespace std;
class Date
{
private:
int day, month, year;
public:
Date(int inMonth, int inDay, int inYear)
:month(inMonth), day(inDay), year(inYear)
{
cout << "Date is created!" << endl; // I put this in to see the constructor work
}
Date& operator ++ () // when I leave off the & the result is the same, why would I use it?
{
++day;
return *this;
}
Date& operator -- () // when I leave off the & the result is the same, why would I use it?
{
--day;
return *this;
}
void DisplayDate()
{
cout << month << " / " << day << " / " << year << endl;
}
};
int main()
{
Date holiday(12, 25, 2016); // Dec 25, 2016
cout << "The date object is initiated to: ";
holiday.DisplayDate();
++holiday; // move data ahead by a day
cout << "Date after prefix-increment is: ";
holiday.DisplayDate();
--holiday; // move date backwards by a day
cout << "Date after a prefix-decrement is: ";
holiday.DisplayDate();
return 0;
}
In the lines in question (line 16, 22), Date& is the return type if the function.
If you aren't using the result of the function, then its return type doesn't matter.
Do you know what the difference between passing something by 'value' and by 'reference' is?
That is the same difference here, except it's the return type and not a parameter.
Notice how the ++t2_ref call also updated the original object (t2), but the ++t1_copy call didn't. This is because the ++ operator in ThingPlusCopy creates a copy of the original object, which no longer affects the original t1.
references are often used for efficiency. Without the reference, the computer has to copy the info, and when that info is a class (including string, vector, and other container objects), that copy can be a fair amount of work. With the reference, it just uses the original, without copying it. This can be dangerous, if you did not want to damage the original, so you also need the concept of a constant reference, to prevent modification to the original while getting the performance boost.
the result is the same, but the time taken can be substantial, esp if your function is called a few million times in a loop and each iteration of the loop it has to stop to copy several MB of info. When working with small classes and functions that are only called once or a few times, you will not see the difference.
Thank you for the good example @Ganado I kinda see what it does. Kinda (just) until I work with it more. I do understand the difference between passing by value or reference. However, I do think I need more practice with it more.
Hey, @jonnin super helpful. You nailed it for me. I see what you mean. I am also glad you explained the const part. I think the initial explanation about const were way too detailed. Your explanation hit home and makes me totally understand.
I use a computer for many things like 3D design and video editing. Sometimes while compositing I include files that I have worked hard on. I don't want to mess anything up in these files (kinda like I deem them const). So what I do is create a zip archive of the original before I copy it in. Of course, it is not exactly the same thing because like I said I copy them in. However, the protection concept is something like what const does for references! I get it, friend.