A question about "this" pointer

V

Vols

"this" pointer points to the object for which the member function is
called. My question is :
When we must use a "this" pointer? any examples? thanks
-Vols
 
K

Kai-Uwe Bux

Vols said:
"this" pointer points to the object for which the member function is
called. My question is :
When we must use a "this" pointer? any examples? thanks

1) In the last line of the assignment operator:

A & operator= ( A const & other ) {
...
return ( *this );
}

More generally, whenever you want to return a reference to the current
object.


2) Sometimes this->some_member provides a convenient name to help name
resolution, e.g.:


struct A {

int a ( void ) const {
return ( 3 );
}

};

template < typename Base >
struct B : public Base {

int b ( void ) const {
return ( this->a() );
}

};

int main ( void ) {
B<A> b_obj;;
b_obj.b();
}


Best

Kai-Uwe Bux
 
M

Maxim Yegorushkin

"this" pointer points to the object for which the member function is
called. My question is :
When we must use a "this" pointer? any examples?

1) When you want to access a member hidden by a local variable with
the same name:

struct X
{
int x;

void foo(int x)
{
x; // refers to local x

this->x; // refers to X::x
// or
X::x; // refers to X::x
}
};

2) When accessing a member of a template argument dependent base class
(http://www.comeaucomputing.com/techtalk/templates/
#whymembernotfound):

template<class T>
struct Y
{
int y;
};

template<class T>
struct X : Y<T>
{
void foo()
{
this->y; // refer to Y<T>::y
// or
Y<T>::y; // refer to Y<T>::y
}
};
 
Z

Zachary Turner

1) When you want to access a member hidden by a local variable with
the same name:

    struct X
    {
        int x;

        void foo(int x)
        {
            x; // refers to local x

            this->x; // refers to X::x
            // or
            X::x; // refers to X::x
        }
    };

FWIW, I always use this-> when accessing ANY member variable, I think
it increases readability since the reader of the code can give entire
pages of source code a quick once-over and immediately know where all
member access are.
 
M

Maxim Yegorushkin

FWIW, I always use this-> when accessing ANY member variable, I think
it increases readability since the reader of the code can give entire
pages of source code a quick once-over and immediately know where all
member access are.

I do always use this for calling member functions. I'd love do the
same for member variables, being too lazy however, I am content with
using trailing underscore to denote member variables.
 
A

amrollahi.saeed

"this" pointer points to the object for which the member function is
called. My question is :
When we must use a "this" pointer? any examples? thanks
-Vols

Hi
Besides what others wrote, I think "method chaining" is the most
important application of this pointer. Consider the following code:

class C {
public:
C& f() { /* ... */ return *this; }
C& g() { /* ... */ return *this; }
C& h() { /* ... */ return *this; }
};

// use C
C obj;
obj.f().g().h(); // method chaining
One primary example of method chaining is in assignment operator.
Of course you asked when we --must-- use a this pointer?
You don't have to use method chaining, but as friends wrote
"name resolution" is one of them. Consider the following code:
class C {
int m;
public:
void set_m(int m) { this->m = m; } // you must use "this"
};
You must use this to distinguish C::m and argument m.
Using this can help to improve readability.

Regards,
-Saeed Amrollahi
 
A

amrollahi.saeed

[..]  Consider the following code:
class C {
  int m;
public:
  void set_m(int m) { this->m = m; } // you must use "this"
};
You must use this to distinguish C::m and argument m.
Using this can help to improve readability.

I'm reading those "reasons" to use 'this' and shudder.  There is no need
to use 'this' here.  Just don't name the local variable the same as any
member (or vice versa).  Readability actually improves with *less* code
written (the 'this->' is *code*).  Of course, readability is in the eye
of the beholder.  But still...

V

What I mean is, according to language rules, both m has somehow local
scope. More
precisely, one m has class scope and another one has local scope. If
programmer
have to (I don't know how or why!) to use m for both variables, he
must use
this pointer. I used the example from ARM book. Of course I agree with
you,
using same identifier for two variables is not a good experience and
should be
avoided.
Why you shudder by those "reasons"? :)
 
J

James Kanze

When accessing a dependent member function or variable.

Only if they are members of a dependent base class. You never
need this-> to access your own members (which are in the third
category: names declared within the template).

More rigorously, using this-> is one way of forcing the
following name to be dependent (and use dependent name lookup).
Of course, it can only be used for members; I don't know of any
way of forcing the name of a non-member to be dependent
(assuming it isn't naturally dependent).
 
R

red floyd

Only if they are members of a dependent base class.  You never
need this-> to access your own members (which are in the third
category: names declared within the template).

More rigorously, using this-> is one way of forcing the
following name to be dependent (and use dependent name lookup).
Of course, it can only be used for members; I don't know of any
way of forcing the name of a non-member to be dependent
(assuming it isn't naturally dependent).

Of course, you're right. My wording was incorrect, but my intent
was what you said. That's why I pointed to the FAQ, which was a
lot clearer than I was.
 
G

gw7rib

"this" pointer points to the object for which the member function is
called. My question is :
When we must use a "this" pointer? any examples? thanks

I offered the following example in response to a similar question a
bit ago.

Suppose you have a class called Thing, which includes two pointer
members called next and previous. Suppose you have a function tieup,
which ties up two Things in the sense that the first one's "next"
pointer points to the second Thing, and the second ones "previous"
pointer points to the first Thing. We can do this as follows:

void tieup(Thing *first, Thing *second) {
first -> next = second;
second -> previous = first;
return;
}

However, this means an external function - tieup - has to be able to
mess about with the internal variables of a Thing. Suppose your boss
does not like this - he thinks Things should be "encapsulated" and
that this sort of thing should be handled by a member function. So you
make one as follows:

void Thing::tieupwith(Thing *nextone) {
next = nextone;
nextone -> previous = this;
return;
}

And, unless I've made an error,

first -> tieupwith(second);

should have the same effect as

tieup(first, second);


Does that help?

Paul.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,265
Latest member
TodLarocca

Latest Threads

Top