By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,918 Members | 1,247 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,918 IT Pros & Developers. It's quick & easy.

What is *this ?

P: n/a

Hi! I would like to know what *this is .
I see *this in structs and classes. I even
saw *this in a linked list but I know almost
nothing of a linked list except being used
to dynamically store data somehow. One
thing I do know about *this thing ... It's
a pointer. Thanks people :-)

Regards
Me

Jul 31 '05 #1
Share this Question
Share on Google+
15 Replies


P: n/a
in*************@yahoo.com wrote:
Hi! I would like to know what *this is .
I see *this in structs and classes. I even
saw *this in a linked list but I know almost
nothing of a linked list except being used
to dynamically store data somehow. One
thing I do know about *this thing ... It's
a pointer. Thanks people :-)


int main()
{
int i = 10;

int *p = &i;
*p = 20;

std::cout << i; // 20
}

On the line "*p = 20", you are assigning a new value to 'i'. The same
thing for

class C {};

int main()
{
C c, d;

C *p = &c;
*p = d;
}

And since in a member function 'this' points to the object on which the
function was called,

void C::f()
{
*this = something;
}

is the same thing as

void f(C *c)
{
*c = something;
}
Jonathan

Jul 31 '05 #2

P: n/a
Can you be more explicit with this ? I'm still new to c++ and I would
like
to know more on how the this pointer can be used. thanks

Jul 31 '05 #3

P: n/a
in*************@yahoo.com wrote:
Can you be more explicit with this ? I'm still new to c++ and I would
like
to know more on how the this pointer can be used. thanks


Please quote the message you are answering to.

The this pointer points to the object on which you called the member
function. We usually say it is an implicit parameter, though it is not
necessarily implemented this way.

class C
{
int i;

public:
void f()
{
i = 10;
}
};

can be thought as (and is usually implemented as)

class C
{
int i;

public:
void f(C * const this)
{
this->i;
}
};

So the this pointer is an ordinary pointer, although it cannot be made
to point elsewhere.
Jonathan

Aug 1 '05 #4

P: n/a
>Please quote the message you are answering to.

ohhhh! I see it is like pointing to a function name. I'm new at posting
and I will quote as needed from now on. Thanks johnathan. I'm going
to post about linked lists as a new topic in comp.lang.c++ soon.
By the way.....
Thanks much
Me

Aug 1 '05 #5

P: n/a
in*************@yahoo.com wrote:
Please quote the message you are answering to.
ohhhh! I see it is like pointing to a function name.


No, it is not a pointer to a function, it is a pointer to an object. It
points to the object on which you called the function:

# include <iostream>

class C
{
public:
void f()
{
std::cout << this;
}
};

int main()
{
C c;
std::cout << &c;
}

The addresses output will be the same.
I'm new at posting
and I will quote as needed from now on.


Well you should quote the parts you are answering to. If your answer
applies to the whole post, quote it all. If you think the quote will be
too long, try to snip the less relevant parts. If you answer only to
specific parts, insert your answers in the quote (as I just did).
Jonathan

Aug 1 '05 #6

P: n/a
in*************@yahoo.com wrote:
Hi! I would like to know what *this is .
I see *this in structs and classes. I even
saw *this in a linked list but I know almost
nothing of a linked list except being used
to dynamically store data somehow. One
thing I do know about *this thing ... It's
a pointer. Thanks people :-)

Regards
Me


Another way to think of "this" is as an implicit member of every object.
So if you define:

class C
{
void f() {do something;}
void g() {do someting else;}

int x; // some value
float y; // some other value
};

what you really get is equivalent to (at least conceptually):

class C
{
void f() {do something;}
void g() {do someting else;}

int x; // some value
float y; // some other value
C* const this; // see below
};

Here, "this" is a constant pointer to a C object whose value (i.e., the
address stored in the pointer) is always automatically set to the
address of the particular C object.

Mark
Aug 1 '05 #7

P: n/a
*this is two things. The * part is a dereference operator, and the latter
the self-referencing pointer.

If you are not familiar with the dereference operator you need to read some
text.

Consider:

class circle
{
public:
double radius;

double area()
{
return 3.14 * radius * radius;
}
};

circle c1;
circle c2;
c1.radius = 3;
c2.radius = 4;
double a1 = c1.area();
double a2 = c2.area();

Now notice the function circle::area(). How does the function know from
which object is the 'radius' variable from? The answer is: it is deduced
from the this pointer. Conceptually, it is translated to something like
this:

double circle::area(circle* this)
{
return 3.14 * ((*this).radius) * ((*this).3.14);
}

so the invocation of c1.area() is really:

double a1 = circle::area(&c1);

Regards,
Ben
Aug 1 '05 #8

P: n/a
>Ben wrote:
Now notice the function circle::area(). How does the function know from
which object is the 'radius' variable from?


Ok, I just hacked up a simple radius to area program similar to Bens.
My program knows about which object radius is from by using these two
methods void SetRadius(double) and double GetRadius() . I know we can
also use the dot operator to obtain a 'public' variable or use the
scope
resolution operator. Here is my code and maybe we could figure out
how to use *this with it ? Thanks much guys/gals !

#include<iostream>
#include<stdlib.h>

class Circle
{
private:
double radius ;

public:

Circle() ;
double FindArea() ;
void SetRadius(double) ;
double GetRadius() ;
~Circle() ;
} ;

Circle::Circle()
{
radius = 0 ;
}

Circle::~Circle()
{
}

void Circle::SetRadius(double RadiusVal)
{
radius = RadiusVal ;
}

double Circle::GetRadius()
{
return radius ;
}

double Circle::FindArea()
{
return 3.14 * radius * radius ;
}
int main(int argc, char *argv[])
{
Circle mycircle ;
int rad = 0 ;
system("cls") ;
std::cout << "Enter the value of the radius for this circle to find
its area : " ;
std::cin >> rad ;
mycircle.SetRadius(rad) ;
std::cout << "The circle with a radius of " << mycircle.GetRadius() <<
" has an area of : " << mycircle.FindArea() ;

return 0 ;

}

Aug 1 '05 #9

P: n/a
I still can't understand how I can use *this...

Regards
Me

Aug 2 '05 #10

P: n/a
in*************@yahoo.com wrote:
I still can't understand how I can use *this...

Regards
Me


You generally don't. Unless you want to return the current object.
Aug 2 '05 #11

P: n/a
>You generally don't. Unless you want to return the current object.

By returning the current object do you mean

struct something
{
int anynumber ;
float anyfloat ;
something *this ;
} ;

So something *this returned the value of anynumber and anyfloat at the
same time ?

thanks
me

Aug 2 '05 #12

P: n/a
Second thought I'm not going to try and understand the this pointer
anymore.
It was something I wanted to understand but maybe I'm not ready for it
now.
I appreciate your help :)

Regards,
Me

Aug 2 '05 #13

P: n/a

<in*************@yahoo.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
You generally don't. Unless you want to return the current object.


By returning the current object do you mean

struct something
{
int anynumber ;
float anyfloat ;
something *this ;
} ;

So something *this returned the value of anynumber and anyfloat at the
same time ?

thanks
me


You can find a lot of examples in overloaded operators. Consider the
expression

++x

which not only increments x but also returns a copy of incremented x. If x
is of type some_type, then the ++ operator can be implemented like this:

some_type& some_type::operator ++ (void)
{
increment();
return *this;
}

Regards,
Ben
Aug 2 '05 #14

P: n/a
Ben wrote:
which not only increments x but also returns a copy of incremented x.

So that would be why when y=0 and x = 1 then y = ++x , y = 2 now
because
the value that is returned from ++ can be assigned to both x and y .
Sorry about the wording on that one.... It's confusing to think about
:-)

Sincerelly ,
Me

Aug 2 '05 #15

P: n/a
Oops this is what Ben wrote...
which not only increments x but also returns a copy of incremented x.

sorry

Aug 2 '05 #16

This discussion thread is closed

Replies have been disabled for this discussion.