Return this pointer vs Pointer to class?

What is the difference if i use the return this pointer and creating a pointer to class object? They both save some memory space.

say i have a code like this

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
struct Tuna{

	int x = 50;
	 Tuna() : x(200){
		
	}

	 Tuna &foo(){
		 return *this; //return type is reference to Tuna class
	 }


	 /****************
	 
	 Returns a new object made by copying the current object

	 *****************/
	 Tuna bar(){ 
		 return *this;
	 }
	 void set(int value) { x = value; }

};
int main(){

	// a pointer to class
	Tuna tt;
	Tuna *foobar = &tt;
	foobar->set(20);
	cout << foobar->x << endl;



	Tuna t;
	t.foo().set(1);
	cout << t.x << endl;




	system("pause");

	return 0;

}


They are just the same. What are the advantage of using one over the other?
Or is it just a programmer's preferencce?



EDIT: sorry about that. i submit it early. I edited my code.

What i mean by the same is that you can call function from Tuna class with either using the return *this pointer or creating a pointer to class. So whats the difference?

here is the output

https://ideone.com/VOYvpp
Last edited on
What are the same?

You never show the result of line 39.
Are you sure they both produce the same output? Take a look again:

1 Original is: 1
200 Original is: 200


The original value of x for any Tuna object is 200, which one was modified?
I update my post..
closed account (z05DSL3A)
I'm a bit confused by the '...return this pointer and creating a pointer to class object...' and then giving code that returns a reference and a copy.

Okay i am more confuse now. I really dont understand the point of using return this pointer..

Can you give me a reason why would i return a this pointer?
closed account (z05DSL3A)
Can you give me a reason why would i return a this pointer?


If I understand you...

return *this; is not returning a this pointer. this itself is a ponter, so *this is dereferencing the pointer to get the object. Depenting on the return type of the function you then either return a reference to the object or a temporary copy of the object.
here's a use case t.foo().set(1);
it would be more pretty if 'foo()' actually does something

1
2
3
4
5
6
7
8
// http://www.parashift.com/c++-faq/named-parameter-idiom.html
File f = OpenFile("foo.txt")
           .readonly()
           .createIfNotExist()
           .appendWhenWriting()
           .blockSize(1024)
           .unbuffered()
           .exclusiveAccess();
@Canis, @ne555

So whats the value of the dereference this? Is it the object t?

I can understand a code like this

int i = getsomeanswer(25);

1
2
3
4
getsomeanswer(int x){

return x;
}


But not a code like this

1
2
3
getsomeanswer(){
return *this
}


Last edited on
this is a pointer to the calling object
so *this is the calling object

1
2
3
//equivalent to t.foo().set(1);
t.foo();
t.set(1);
this is a pointer that is implicitly created for each member function.

When you have:
1
2
3
4
5
6
7
8
9
10
11
12
13
struct Foo {
  int x = 42;
  void foo( int a ) {
    x += a;
    // you could be more explicit and write here:
    // this->x += a;
  }
};

int main() {
  Foo bar;
  bar.foo( 7 );
}


One could write equivalent (pseudo)code:
1
2
3
4
5
6
7
8
9
10
11
12
struct Foo {
  int x = 42;
};

void foo( Foo * this, int a ) {
  this->x += a;
}

int main() {
  Foo bar;
  foo( &bar, 7 );
}
OK i see..

But i still cant understand the part where when i created a Tuna object on my main

Tuna t;

Why do i need to return the Tuna itself on the object where is also a Tuna object from the main class?



t.foo(); // here is the part that bugs me. Why do i need to return a Tuna class from the object where it is also a Tuna object named t?

foo() is returning the Tuna class to the object t? but why? t is already a Tuna object.
Last edited on
closed account (z05DSL3A)
You don't have to return a reference to the object but it can be beneficial.

Lets say you have your Tuna object t and you want to call foo() and then bar() on it.

if foo and bar return void you would have to write:

1
2
t.foo();
t.bar();


if you return a reference to the object you can then chain the calls:

t.foo().bar();

You have already made use of chaining (without realising it) in code such as cout << t.x << endl;. Syntactic sugar hides the fact that << is a function.

These two lines of code are the same:
1
2
std::cout << x << " " << y << std::endl;
std::cout.operator<<(x).operator<<(" ").operator<<(y).operator<<(std::endl);


_____________________________________
Edit:
another way to think about it is with substitution. In t.foo().bar();, you can substitute the call f.foo() with its returned object. If foo() returned void you would then efectivly have (void).bar();. When you return a reference to the object that contains the function you would have t.foo() replaced with t so you would still have t.bar(); which is valid.
Last edited on by Canis lupus
FINALLY! After a day or two finding some answers. I finally understand it now..

Thank you very much..

So far i have not come up with an idea that i can use this. Its really cool but i cant think of some simple app that can use this.

But i understand it now..

Thank you very much
Topic archived. No new replies allowed.