Overloaded Operators

Hello,

I am having an issue getting my overloaded postfix operators ++ and -- to work inside my class. Here is my entire class:

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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
class NumDays
{
	private:
		int hours;
	public:
		NumDays();
		NumDays(int);
		NumDays(double);

		void setHours (int);
		void setDays (double);
		int getHours();
		double getDays();
		operator double(); 
		NumDays operator+ (NumDays&);
		NumDays operator- (NumDays&);
		NumDays& operator++ ();
		NumDays& operator-- ();
		NumDays operator++ (int);
		NumDays operator-- (int);
};

//********************************************

NumDays::NumDays()
{
	hours = 0;
}

//********************************************

NumDays::NumDays (int time)
{
	hours = time;
}

//********************************************

NumDays::NumDays (double days)
{
	hours = days * 8;
}

//********************************************

void NumDays::setHours (int numHours)
{
	numHours = hours;
}

//********************************************

void NumDays::setDays (double d)
{
	hours = d * 8;
}

//********************************************

int NumDays::getHours ()
{
	return hours;
}

//********************************************

double NumDays::getDays ()
{
	double days;
	days = (hours / 8);
	return days;
}

//********************************************

NumDays::operator double ()
{
	double x;
	x = hours/8.0;
	return x;
}

//********************************************

NumDays NumDays::operator+ (NumDays& a)
{
	NumDays b;
	b.hours = this->hours + a.hours;
	return b;
}

//********************************************

NumDays NumDays::operator- (NumDays& a)
{
	NumDays b;
	b.hours = this->hours - a.hours;
	return b;
}

//********************************************

NumDays& NumDays::operator++ ()
{
	++hours;
	return *this;
}

//********************************************

NumDays& NumDays::operator-- ()
{
	--hours;
	return *this;
}

//********************************************

NumDays NumDays::operator++ (int)
{
	hours++;
	return hours;
}

//********************************************

NumDays NumDays::operator-- (int)
{
	hours--;
	return hours;
}


Whenever I use this class and use the ++ postfix operator, it acts like a prefix operator. What could I do to fix my code to get the postfix operator working?

Thanks for any and all help!
Whenever I use this class and use the ++ postfix operator, it acts like a prefix operator.


That's because the two are doing the same thing ;P

The postfix operator needs to remember the "old" value and return that. This is usually done by making a copy of it:

1
2
3
4
5
6
NumDays NumDays::operator++ (int)
{
  NumDays copy( *this );
  ++hours;
  return copy;
}


This is also why the postfix operators can't return a reference like the prefix operators can -- because they have to return that copy and not *this.
Thank you for your help! I really appreciate it. I have a question regarding the NumDays copy( *this ); line. What exactly does this line do? Is it creating an object of type NumDays called copy. If so, what is the *this pointer filling in?

Thanks again.
Its making a copy of the NumDays object the postfix operator is being used on
Topic archived. No new replies allowed.