i cant understand this

i copied a lot of code for practice and i come up with this problem.

why is the purpose of the "&" next to the first vec3 i deleted it and still works and know what does & but i dont figure out what is hes purpose here.

the function is returning the memory address of the current object why put & there

[code]i only put this part because is a lot of code.

in vec3.cpp:

vec3"&"vec3::Divide(const vec3 & other)
{
x /= other.x;
y /= other.y;
z /= other.z;
return *this;
}

in vec3.h:
vec3"&"Divide(const vec3 & other);
The & can be three different things, depending on where it is:
* operator AND
* "address of" -operator
* a reference

Which could it be here?
i think it should be a reference but a reference to ??? that is what a dont understand ,
a reference to ¿¿this*??? what this means ,isnt *this the address of the current object ?
How is a member object used? it is called on some object:

1
2
3
4
5
vec3 foo;
vec3 bar;
// set values

/*something*/ = foo.Divide( bar );

During that function call the this points to foo.
Therefore, *this is foo.
Function thus returns a reference to foo (this time).
Please use code tags. http://www.cplusplus.com/articles/jEywvCM9/

JmmL wrote:

that is what a dont understand ,
a reference to ¿¿this*??? what this means ,isnt *this the address of the current object ?
this is a pointer to the current object.
*this is dereferencing the pointer, and thus refers to the current object.

Returning a reference to the current object allows you to chain operations like so:
1
2
vec3 v1{ 1.f, 2.f, 3.f }, v2{ 4.f, 5.f, 6.f }, v3{ 7.f, 8.f, 9.f };
v1.Divide( v2 ).Divide( v3 );
Topic archived. No new replies allowed.