Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where does the reference points to?

I'm not sure if I have understood the stack right. I have the following operator overloading for the complex numbers a and b (a=3+5i and b=2+i).

struct complex{
int x;
int y;
};

complex& operator+=(complex& a, const complex b){
a.x=a.x+b.x;
a.y=a.y+b.y;
return a; 
}

now i wonder where does the reference for the return value points to.

I think in the main stack frame there is a memory area for a = a.x and a.y of 64 Bits because a.x/a.y are of type int. And the return value a in the operator+=stack-frame points to this "a"-memory area.

I wonder how the "a"-memory-area looks like and how is an object of the type complex is stored in the main-stack-frame?

Is it like an array and the reference points to "a[0]“ or are a.x and a.y separeted and you need "two" reference-pointers to point to an object of type complex.

like image 430
Suslik Avatar asked Oct 19 '22 21:10

Suslik


1 Answers

a is a reference to the variable used to invoke the operator += with.

complex w, p;

void f() {
  w.x = w.y = 0;
  p.x = 1; 
  p.y = 0;
  w += p;
}

the parameter a on operator += is an alias for w, and the parameter b is an alias for p.

like image 180
vz0 Avatar answered Oct 28 '22 20:10

vz0