strange use of this pointer

P

pauldepstein

Suppose Member is a member of a class called Class, and Function is a
function belonging to that class. Then, is there any reason to
write this->Member within Function? Surely, just writing Member
means the same thing and is clearer?

Thanks,

Paul Epstein
 
J

Jerry Coffin

Suppose Member is a member of a class called Class, and Function is a
function belonging to that class. Then, is there any reason to
write this->Member within Function? Surely, just writing Member
means the same thing and is clearer?

There are a few situations (mostly in templates) where this->member make
sense. Otherwise, most use is a leftover from some other language where
it doesn't happen automatically.
 
S

Saeed Amrollahi

Suppose Member is a member of a class called Class, and Function is a
function belonging to that class.  Then, is there any reason to
write    this->Member    within Function?  Surely, just writing Member
means the same thing and is clearer?

Thanks,

Paul Epstein

Hi

I think one (minor) application of this pointer is when the arument
name of Function is same as Member:
class Class {
Sometype Member;
Function(Sometype Member) { this->Member = Member; }
};

In this situation, using this, I distinguish two Members. Of course it
isn't a good practice and should be avoided. I don't know why such
coding idiom is common in other languages like Java and C#?

Regards,
S. Amrollahi
 
D

D. Susman

In an implementation having a considerably lengthy number of local/
member variables and methods, you may want to indicate whether the
variable used is a member/local variable by using "this->".
 
A

Andrey Tarasevich

Suppose Member is a member of a class called Class, and Function is a
function belonging to that class. Then, is there any reason to
write this->Member within Function? Surely, just writing Member
means the same thing and is clearer?

Aside from some [already mentioned] contexts in template programming,
writing 'this->Member' might be necessary when the member name is hidden
by a parameter name

void SomeClass::Function(int Member)
{
this->Member = Member;
}

Also, some might prefer to use 'this->Member' all the time as a
convention, which is supposed to emphasize the fact the we are actually
accessing a class member, not some non-member (file-scope or automatic)
object.
 

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
474,176
Messages
2,570,949
Members
47,500
Latest member
ArianneJsb

Latest Threads

Top