473,569 Members | 2,688 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 1360
i think so in this case.

"psp" <pr************ *@gmail.com>
??????:11****** *************** *@h2g2000hsg.go oglegroups.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.comwrot e:
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_typ e"
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_m ethod();
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::sta tic_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*Intelli sense*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.c om/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.t elia.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
2242
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 classes A, ... typedef A * APtr;
10
2133
by: solosnake | last post by:
Whilst browsing Flipcode I noticed this code: class CConsole: public I_TextOutput { public: ... void Release() { delete this; } ... };
3
1562
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 BredthFirstPrint and make both approriate func ptrs it works fine. void print(void *data); #define R 'r'-'r' #define S 's'-'r'
32
3155
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
1267
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 here? Thanks! private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); }
2
11778
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, and sometimes don't. Just wonder what is the meaning of this? Do I need to concern of any wrong doing in my program or codes? Thanks in advance.
7
2216
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 me. So, I'm here for an answer (more of a confirmation), hopefully. First let me say that I know people keep saying; it doesn't work because the...
60
5003
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. consistently may make the code easier to understand for someone else, etc. Using "this." makes it immediately clear, for example, that the variable being...
10
4784
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 if value of the current field is not the same value of the next row. eg:
3
1367
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
7703
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7618
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
1
7679
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7983
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6287
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5223
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3657
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3647
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
946
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.