Usin *this with Class and Class&

Can someone approve my theory.

If we use *this with function
1
2
3
4
5
Screen &Screen::move(pos r, pos c){
	pos row = r * width;
	cursor = row + c;
	return *this;
}

and
1
2
3
4
5
Screen Screen::move(pos r, pos c){
	pos row = r * width;
	cursor = row + c;
	return *this;
}

it is basiclly the same thing, just in first case we don't copy entire object instead we just go to that address that it holds?
In first example we are returning reference to the object. That means all further operations on returned value would happen on original object
1
2
Screen S;
S.move(10, 10).move(1, 1);  //Moved to the 1, 1 position 


In second example, copy of current object is returned. All operations will change (unnamed) copy.
1
2
Screen S;
S.move(10, 10).move(1, 1);  //Moved to the 10, 10 position, second move is essentually no-op (assuming no side effects) 

Thank you!
Last edited on
Topic archived. No new replies allowed.