why is "this->" necessary sometimes?

P

psp

I'm working on some code and I see in many methods that call other
methods of the same class prefix a this-> to the method being called,
i.e.

class X::methodA()
{
this->methodB();
}

And they are not even static methods. Isn't the this-> redundant?
Please explain.
Thank you.
 
B

blangela

I'm working on some code and I see in many methods that call other
methods of the same class prefix a this-> to the method being called,
i.e.

class X::methodA()
{
this->methodB();

}

And they are not even static methods. Isn't the this-> redundant?
Please explain.
Thank you.

In the example code above I assume you meant to type "return_type"
instead of "class"?

You are correct in that the "this->" part of the invocation of
methodB() is not required (assuming methodB() belongs to the same
class as methodA(), or a base class of methodA() ). It does document
to anyone reading the code that methodB() is being invoked with the
same object that invoked methodA(). This is the same reason that some
C++ programmers will invoke a global function like this:

::global_func(): // The :: operator signals that global_func() is not
a method of a class.

You mention that they are not even static methods. An interesting
comment! In my C++ courses I teach that it is extremely bad
programming form to invoke a static member function in any of the
following ways:

object.static_method();
ptr->static_method();
this->static_method();

Why? Because all 3 invocations give the casual observer the
impression that static_method() is normal method rather than a static
method. Instead it should be invoked in this way:

class_name::static_method();

I hope that answers your question.

Cheers,

Bob
 
R

red floyd

psp said:
I'm working on some code and I see in many methods that call other
methods of the same class prefix a this-> to the method being called,
i.e.

class X::methodA()
{
this->methodB();
}

And they are not even static methods. Isn't the this-> redundant?
Please explain.

It's not necessary in most cases. I suspect that they are there to make
elements of the IDE (*cough*Intellisense*cough*) easier to use (restrict
to members only).

However, when dealing with templates, sometimes this-> is required. See
FAQ 35.19 http://www.parashift.com/c++-faq-lite/templates.html#faq-35.19
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

I'm working on some code and I see in many methods that call other
methods of the same class prefix a this-> to the method being called,
i.e.

class X::methodA()
{
this->methodB();
}

In most cases it's redundant, however there are some cases where it can
be useful, since it can clarify the intent if nothing else.

class Foo {
int i;
public:
void set(int i) {
this->i = i;
}
};

I don't know if the compiler can figure out that one if you don't use
this-> since I always use it in such situations since it makes the code
easier to read.
 
B

benben

psp said:
I'm working on some code and I see in many methods that call other
methods of the same class prefix a this-> to the method being called,
i.e.

class X::methodA()
{
this->methodB();
}

And they are not even static methods. Isn't the this-> redundant?
Please explain.

Assuming it's only a typo of you to write 'class' in place of a type
name, the 'this->' part is indeed redundant.

The only place where you have to specify 'this->' is from a class
template calling an inherited member function, for example:

class C
{
protected:
void f() const;
};

template <typename T>
class D: private C
{
void g() const
{
this->f(); // need to specify this->
}
};
Thank you.

Let me know if there are other places where this-> is needed.

Regards,
Ben
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top