Why a reference again?

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
using namespace 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;
}
Last edited on
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.

https://en.cppreference.com/w/cpp/language/operator_incdec

This is an example of actually using the return type of ++:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Example program
#include <iostream>
#include <string>

//
// Pre-increment operator implemented two ways (e.g. ++obj)
//

class ThingPlusRef {
  public:
    ThingPlusRef() : day(0) {}
  	ThingPlusRef& operator ++()
	{
		++day;
		return *this;
	}
	
	int day;
};

class ThingPlusCopy {
  public:
    ThingPlusCopy() : day(0) {}
  	ThingPlusCopy operator ++()
	{
		++day;
		return *this;
	}
	
	int day;
};

int main()
{
    using std::cout;
    
    ThingPlusCopy t1;
    ThingPlusCopy t1_copy = ++t1;
    ++t1_copy;
    
    cout << "t1.day      = " << t1.day << '\n';
    cout << "t1_copy.day = " << t1_copy.day << '\n';
    
    ThingPlusRef t2;
    ThingPlusRef& t2_ref = ++t2;
    ++t2_ref;
    
    cout << "t2.day      = " << t2.day << '\n';
    cout << "t2_ref.day  = " << t2_ref.day << '\n';
}


t1.day      = 1
t1_copy.day = 2
t2.day      = 2
t2_ref.day  = 2


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.

PS: Your question is not Windows specific.
Last edited on
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.

I am ecstatic to understand this now :)
Topic archived. No new replies allowed.