Problems with this pointer

Hey,

i´m a student and I´m currently learning c++ and only recently I started programming the class Komplex in order to get a little routine. So far everything worked quite well but now I came to a point in which I tried to overload differnt operators.
In the sourcecode which can be found at the end of my post I first declared all my methods in the private part of the header as usual.
Following this, I tried to overload the plusoperator multiple times in a differnt way. So everything works fine but now a couple of questions have appeared.
In our lecture we once overloaded the += operator with the this pointer. As you can see I did as well, but unfortunately I don´t have any clue what he does in this context. I´m aware that the this pointer is basically a pointer which points to an object of my class Komplex but now I just don´t get why I can use the pointer in this way.

1
2
3
4
5
Komplex Komplex::operator+=(Komplex a)
{
	*this = *this + a;
	return *this;
}


I basically take the this pointer an add up the object a.
So this is exactly where my problems appear. Why can I just add the this pointer with my object a. Wherefrom does the compiler know that he has to add the realpart and the imaginary part of both objects becaus this is exactly what he does when I run the programme.



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

#define PI 3.1415
#include <iostream>
#include <string>
#include <math.h>
using namespace std;
class Komplex
{
	private:
		string name;
		float re;
		float im;
		float be;
		float phi;
		
	public:
		void eingabe();
		void ausgabe();
                void add(const Komplex &a, const Komplex &b);	
		void sub(const Komplex &a, const Komplex &b);	
		void mul(const Komplex &a, const Komplex &b);
		void div(const Komplex &a, const Komplex &b);
		
		Komplex operator+=(Komplex a);
		Komplex operator-=(Komplex a);
		Komplex operator*=(Komplex a);
		Komplex operator/=(Komplex a);
		
		
		friend Komplex operator+(Komplex a, Komplex b);
		friend Komplex operator-(Komplex a, Komplex b);
		friend Komplex operator*(Komplex a, Komplex b);
		friend Komplex operator/(Komplex a, Komplex b);
	
		Komplex(string na, float r, float i);
};

Komplex::Komplex(string na, float r, float i)
{
	name = na;
	re = r;
	im = i;
	be = sqrt(re*re + im*im);
	phi = atan2(im,re)*360/(2*PI);
}



Komplex operator+(Komplex a, Komplex b)
{
   Komplex u("u",0,0);                                        
   u.re =  a.re + b.re;       							
   u.im =  a.im + b.im;
   u.be = sqrt(u.re*u.re + u.im*u.im);
   u.phi = atan2(u.im,u.re)*360/(2*PI);
   return u;         
}



  Komplex Komplex::operator+=(Komplex a)
{
	*this = *this + a;
	return *this;
}



void Komplex::add(const Komplex &a, const Komplex &b)
{
	re = a.re + b.re;
	im = a.im + b.im;
	be = sqrt(re*re + im*im);
	phi = atan2(im,re)*360/(2*PI);
}
*this + a;

On left, an object of type Komplex (NOT a pointer to an object of type Komplex - the pointer is being dereference so on the left is an object of type Komplex).
On right, an object of type Komplex.

Does the operator + , with two Komplex parameters, exist?

1
2
3
4
5
6
7
8
9
Komplex operator+(Komplex a, Komplex b)
{
   Komplex u("u",0,0);                                        
   u.re =  a.re + b.re;       							
   u.im =  a.im + b.im;
   u.be = sqrt(u.re*u.re + u.im*u.im);
   u.phi = atan2(u.im,u.re)*360/(2*PI);
   return u;         
}
Yes. There it is.

That's the code being run.

Thank you for your quick reply.

So in this context this means, that the only way to overload the += opeator with the this pointer is that the + opeator has already been overloaded?
The operator+= does whatever is in the function written for it:
1
2
3
4
  Komplex Komplex::operator+=(Komplex a)
{
	// CODE GOES HERE
}

So long as CODE GOES HERE is code that works, this operator += can be used. For example, if this was the code:
1
2
3
4
  Komplex Komplex::operator+=(Komplex a)
{
	cout << "BEans on toast";
}

then you could call operator+= without there being an operator+ defined.

If you want to call operator+ from somewhere (for example, from inside operator+=) then that code needs to have been written. Just like calling any function; you can only call a function that exists.
Topic archived. No new replies allowed.