473,507 Members | 12,744 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

why is "this->" necessary sometimes?

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.

May 11 '07 #1
7 1353
i think so in this case.

"psp" <pr*************@gmail.com>
??????:11**********************@h2g2000hsg.googleg roups.com...
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.

May 11 '07 #2
On May 11, 10:53 am, psp <pradhan.push...@gmail.comwrote:
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

May 11 '07 #3
psp wrote:
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-lit...html#faq-35.19
May 11 '07 #4
Paolo Maldini wrote:
i think so in this case.
Please don't top-post. Your replies belong following or interspersed
with properly trimmed quotes. See the majority of other posts in the
newsgroup, or the group FAQ list:
<http://www.parashift.com/c++-faq-lite/how-to-post.html>
May 11 '07 #5
On 2007-05-11 19:53, psp wrote:
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.

--
Erik Wikström
May 11 '07 #6
"Erik Wikström" <Er***********@telia.comwrote in message
news:81***************@newsb.telia.net...
On 2007-05-11 19:53, psp wrote:
>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.
In some cases it's now necessary, thanks to two-phase name lookup.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
May 12 '07 #7
psp wrote:
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
May 14 '07 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
2232
by: Ernst Murnleitner | last post by:
Dear Readers, Is it possible to forbid conversion from this or use of this in general except where it is explicitly wanted? Reason: I changed my program from using normal pointers to...
10
2124
by: solosnake | last post by:
Whilst browsing Flipcode I noticed this code: class CConsole: public I_TextOutput { public: ... void Release() { delete this; } ... };
3
1558
by: Aryeh M. Friedman | last post by:
Why is the value of "Instance" whacked in the following code. Note this only happens in Graph::*Wrapper. Also please note that if I hardcode WalkFunc and PrintFunct to be BredthFirst and...
32
3142
by: Christopher Benson-Manica | last post by:
Is the following code legal, moral, and advisable? #include <iostream> class A { private: int a; public: A() : a(42) {}
3
1265
by: Polaris | last post by:
I noticed in the ASP.NET web application, as shown below, the "this" pointer is used in the code generated by the Visual C# IDE. Anyone can explain why it is necessary to use the "this" pointer...
2
11763
by: kjon | last post by:
When I try to run any project, sometimes I will encounter this tab window in the IDE which has the following message: "Disassembly cannot be displayed in run mode". I mean it'll show sometimes,...
7
2208
by: relient | last post by:
Question: Why can't you access a private inherited field from a base class in a derived class? I have a *theory* of how this works, of which, I'm not completely sure of but makes logical sense to...
60
4970
by: Dave | last post by:
I'm never quite sure whether to use "this." or not when referring to fields or properties in the same class. It obviously works just fine without it but sometimes I wonder if using this....
10
4777
by: craig.keightley | last post by:
I am trying to get the next row within a loop for a script i am developing... I need to display a final table row within the table that i have displayed on the page, but i only want to show it...
3
1363
by: David d'Angers | last post by:
hi group i read this in the linux version "tree" program struct _info { char *name; char *lnk; u_char isdir : 1; u_char issok : 1; u_char isexe : 1;
0
7221
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7109
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7372
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7481
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5619
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5039
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
411
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.