Alternative to (this)

I have seen a lot of programs using this-> and i just wanted to know why and how it is used and what is the alternative to it.

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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include "upDate.h"
#include <iostream>
using namespace std;

int upDate::count = 0;

upDate::upDate()
{
	iptr = new int[3];
	iptr[0] = 5;
	iptr[1] = 11;
	iptr[2] = 1959;
	count++;
}

upDate::upDate(int M, int D, int Y)
{
	int tempM = 0, tempD = 0, tempY = 0;
	int JD = Greg2Julian(M, D, Y);
	Julian2Greg(JD, tempM, tempD, tempY);

	iptr = new int[3];
	if (M == tempM && D == tempD && Y == tempY) {
		iptr[0] = M;
		iptr[1] = D;
		iptr[2] = Y;
	}
	else {
		iptr[0] = 5;
		iptr[1] = 11;
		iptr[2] = 1959;
	}
	count++;
}

upDate::~upDate()
{
	delete[] iptr;
	count--;
}

upDate::upDate(const upDate &date)
{
	iptr = new int[3];
	iptr[0] = date.iptr[0];
	iptr[1] = date.iptr[1];
	iptr[2] = date.iptr[2];
	count++;
}

void upDate::setDate(int M, int D, int Y)
{
	int tempM = 0, tempD = 0, tempY = 0;
	int JD = Greg2Julian(M, D, Y);
	Julian2Greg(JD, tempM, tempD, tempY);

	iptr = new int[3];
	if (M == tempM && D == tempD && Y == tempY) {
		iptr[0] = M;
		iptr[1] = D;
		iptr[2] = Y;
	}
	else {
		iptr[0] = 5;
		iptr[1] = 11;
		iptr[2] = 1959;
	}
}

int upDate::getMonth()
{
	return iptr[0];
}

int upDate::getDay()
{
	return iptr[1];
}

int upDate::getYear()
{
	return iptr[2];
}

void upDate::displayDate()
{
	cout << *this << endl;
}

string upDate::getMonthName()
{
	string stringMonth = "";
	switch (iptr[0])
	{
	case 1: stringMonth = "January"; break;
	case 2: stringMonth = "February"; break;
	case 3: stringMonth = "March"; break;
	case 4: stringMonth = "April"; break;
	case 5: stringMonth = "May"; break;
	case 6: stringMonth = "June"; break;
	case 7: stringMonth = "July"; break;
	case 8: stringMonth = "August"; break;
	case 9: stringMonth = "September"; break;
	case 10: stringMonth = "October"; break;
	case 11: stringMonth = "November"; break;
	case 12: stringMonth = "December"; break;
	}
	return stringMonth;
}

int Greg2Julian(int month, int day, int year)
{
	int JD;
	int I = year;
	int J = month;
	int K = day;

	JD = K - 32075 + 1461 * (I + 4800 + (J - 14) / 12) / 4 + 367 * (J - 2 - (J - 14) / 12 * 12)
		/ 12 - 3 * ((I + 4900 + (J - 14) / 12) / 100) / 4;

	return JD;
}

void Julian2Greg(int JD, int & month, int & day, int & year)
{
	int L, N;
	int I, J, K;

	L = JD + 68569;
	N = 4 * L / 146097;
	L = L - (146097 * N + 3) / 4;
	I = 4000 * (L + 1) / 1461001;
	L = L - 1461 * I / 4 + 31;
	J = 80 * L / 2447;
	K = L - 2447 * J / 80;
	L = J / 11;
	J = J + 2 - 12 * L;
	I = 100 * (N - 49) + I + L;

	year = I;
	month = J;
	day = K;
}

// Increment Date by n
void upDate::incrDate(int n)
{
	int jd = Greg2Julian(this->getMonth(), this->getDay(), this->getYear());
	jd += n;
	Julian2Greg(jd, iptr[0], iptr[1], iptr[2]);
}

// Decrement Date by n
void upDate::decrDate(int n)
{
	int jd = Greg2Julian(this->getMonth(), this->getDay(), this->getYear());
	jd -= n;
	Julian2Greg(jd, iptr[0], iptr[1], iptr[2]);
}

int upDate::daysBetween(upDate D)
{
	int JD1 = Greg2Julian(D.getMonth(), D.getDay(), D.getYear());
	int JD2 = Greg2Julian(this->iptr[0], this->iptr[1], this->iptr[2]);

	return (JD1 - JD2);
}

// Amount of upDate objects
int upDate::GetDateCount()
{
	return count;
}

// Overloaded + (upDate + int)
upDate upDate::operator+(int n)
{
	upDate temp(*this);
	temp.incrDate(n);
	return temp;
}

// Friend Overloaded + (int + upDate)
upDate operator+(int n, upDate D)
{
	upDate temp(D);
	D.incrDate(n);
	return D;
}

// Overloaded - (upDate - int)
upDate upDate::operator-(int n)
{
	upDate temp(*this);
	temp.decrDate(n);
	return temp;
}

// Overloaded - ... Days Between (upDate - upDate)
int upDate::operator-(upDate D)
{
	return (this->daysBetween(D) < 1) ? -this->daysBetween(D) : this->daysBetween(D);
}

// Friend Overloaded - (upDate - int)
upDate operator-(int n, upDate D)
{
	upDate temp(D);
	D.decrDate(n);
	return D;
}


// Pre-Increment ++upDate
upDate upDate::operator++(int dummy)
{
	this->incrDate(1);
	return *this;
}

// Post-Increment upDate++
upDate & upDate::operator++()
{
	this->incrDate(1);
	return *this;
}

// Pre-Decrement --upDate
upDate upDate::operator--(int dummy)
{
	this->decrDate(1);
	return *this;
}

// Post-Decrement upDate--
upDate & upDate::operator--()
{
	this->decrDate(1);
	return *this;
}

upDate upDate::operator=(upDate D)
{
	iptr[0] = D.iptr[0];
	iptr[1] = D.iptr[1];
	iptr[2] = D.iptr[2];
	return *this;
}

// Print Julian representation of a date
int upDate::julian()
{
	return Greg2Julian(this->iptr[0], this->iptr[1], this->iptr[2]);
}

// Overloaded == 
bool upDate::operator==(upDate D)
{
	return (this->julian() == D.julian()) ? true : false;
}

// Overloaded <
bool upDate::operator<(upDate D)
{
	return ((this->julian() - D.julian()) < 0) ? true : false;
}

// Overloaded >
bool upDate::operator>(upDate D)
{
	return (this->julian() > D.julian()) ? true : false;
}

// Overload Object Stream
ostream & operator<<(ostream & out, const upDate & D)
{
	upDate temp(D);
	out << temp.iptr[0];
	out << '/';
	out << temp.iptr[1];
	out << '/';
	out << temp.iptr[2];
	return out;
}
a lot of this can be avoided by having more that's. (I am only half kidding).
if you have a class method that takes a variable of the same name as the class member as input, you need to clarify which one you mean with a this, eg

someclass::foo(int x)
{
this->x = x; // sigh.
}

vs. this //haha

someclass::foo(int y)
{
x = y;
}

the above solves a lot of the problems where this becomes excessive. Most of the other places where it is used are either unavoidable or the alternative (usually a form of pointers, might also find a way with :: in some cases) is equally ugly or worse. When calling a function member of yourself, I can't think of a way to avoid it. You may be able to overload all such members with a pointer or reference so you end up with

return (input.julian() > D.julian()) ? true : false;

but that is going to generate a ton of clutter just to save using a this... and there may be places where that just forces you to call something with *this instead of using this->
Last edited on
the alternative may be simply to not use it.

by the way, you've got a leak on .setDate()
¿why do you even use dynamic allocation?
IMO, although it's not shared by a lot of people, explicitly using this whenever possible makes the code clearer. Someone not very familiar with the codebase can look at the code and immediately understand what's happening.
1
2
3
void A::foo(){
    bar(baz);
}
What does that mean? Is it a global variable being passed to a member function? A member being passed to a global function? Without more context it's just meaningless.
I don't disagree exactly, but I am used to IDEs that tell me what they are with a mouse-over. Kind of like how I don't really support hungarian anymore, because the type is right there if you want to know it, adding letters is visual clutter. If you don't have tools that tell you everything, extra clarity is useful.
Topic archived. No new replies allowed.