what is " *this " ?

A few years ago I created a header file to work with fractions. Looking over it again, there are a few parts i don't fully understand what they are doing.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//definitions for operations with a fraction and an integer
fraction fraction::operator =(int i)
{
	return *this = fraction(i);
}
fraction fraction::operator +(int i)const
{
	return *this + fraction(i);
}
fraction fraction::operator -(int i)const
{
	return *this - fraction(i);
}
fraction fraction::operator *(int i)const
{
	return *this * fraction(i);
}
fraction fraction::operator /(int i)const
{
	return *this / fraction(i);
}


in the above segment of code what is *this?
(the fraction(i) is a constructor that turns a type int into a type fraction)

this is a pointer to the current class instance.
so if i had something like:

1
2
fraction A=(2 , 3);
fraction B= A * 3;


*this would be referring to the fraction A?
Last edited on
Yes.
ok thanks!
It's also worth noting that all of the above operator overloads are redundant and unnecessary (unless fraction's ctor is explicit).
not sure what you mean by explicit but my constructors are:

1
2
3
4
5
6
7
8
9
10
11
fraction::fraction(int iNum, int iDen)
{
	iNumerator = iNum;
	iDenominator = iDen;
}

fraction::fraction(int iNumber1)
{
	iNumerator = iNumber1;
	iDenominator = 1;
}


(plus the default constructor)
Last edited on
Those aren't explicit (explicit is a C++ keyword that prevents implicit conversions to the class).

Here's what I mean:

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
class fraction
{
//...
public:
  fraction(int num) {...}  // int -> fraction ctor

  fraction operator + (const fraction& r) const { ... }  // + operator that takes a fraction

  // note:  no + operator that takes an int
};

int main()
{
  fraction a;
  fraction b;

  b = a + fraction(3);  // OK, calls the int->fraction ctor to create a temp fraction
    // then calls the + operator

  b = a + 3;  // Also OK!  Same as above.. implicitly calls the int->fraction ctor
    // then calls the + operator that takes 2 fractions.

  // no need to make a separate + operator to take an int!  The int->fraction ctor takes
  //  care of it automagically
}


Adding the 2nd set of operators to take ints just adds a bunch of duplicate code, which is never a good thing. Duplicate code leads to more bugs, and larger and more confusing code. KISS
thanks that definitely cleans things up. I've removed the extra parts, is there anything else unnecessary that i could possible remove?

Header:
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
class fraction
{
public:
	fraction(){;} //default constructor
	fraction(int, int); // member function: Constructor
	fraction(int); //Constructor: turns an int into a fraction

	//declares operator functions between two fractions
	fraction operator =(const fraction&);
	fraction operator +(const fraction&)const;
	fraction operator -(const fraction&)const;
	fraction operator *(const fraction&)const;
	fraction operator /(const fraction&)const;

	//declares operator functions between an integer and a fraction
	friend fraction operator +(const int, const fraction&);
	friend fraction operator -(const int, const fraction&);
	friend fraction operator *(const int, const fraction&);
	friend fraction operator /(const int, const fraction&);

	//declares comparison operators between two fractions
	bool operator ==(fraction);
	bool operator >=(fraction);
	bool operator <=(fraction);
	bool operator < (fraction);
	bool operator > (fraction);
	bool operator !=(fraction);

	//declares comparison operators between an integer and a fraction
	friend bool operator ==(const int, const fraction&);
	friend bool operator >=(const int, const fraction&);
	friend bool operator <=(const int, const fraction&);
	friend bool operator >(const int, const fraction&);
	friend bool operator <(const int, const fraction&);
	friend bool operator !=(const int, const fraction&);

//delaration for input and output modifiers
	friend std::ostream& operator<<(std::ostream&, const fraction);
	friend std::istream& operator>>(std::istream&, fraction&);
	void Reduce();

	int GetNumerator()const{return iNumerator;}
	int GetDenominator()const{return iDenominator;}
private:
	int iNumerator,			//Numerator Of First fraction as Input by the user
		iDenominator,		//Denominatorof the difference of the two fractions
		gcd();
};
#endif


sample code from each operator

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
//two fractions
fraction fraction::operator +(const fraction& f)const
{
	int nuNumerator = iNumerator * f.GetDenominator() + f.GetNumerator() * iDenominator;
	int nuDenominator = iDenominator * f.GetDenominator();

	return fraction(nuNumerator, nuDenominator);
}

//integer and fraction
fraction operator +(const int i , const fraction& f)
{
	return  fraction(i) + f;
}

//two fractions
bool fraction::operator  <(fraction f)
{
	return iNumerator * f.GetDenominator() < f.GetNumerator() * iDenominator;
}

//integer and fraction
bool operator <(const int i , const fraction& f)
{
	return  fraction(i) < f;
}


i think i need to keep the friend functions because if i were to try
1
2
3
b= 3 +  a;
//instead of
b = a +3;


the code doesn't compile (without the friends).
Last edited on
Topic archived. No new replies allowed.