473,695 Members | 2,894 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

not "this" again

I have read the years-old threads on this topic, but I wanted to
confirm what they suggest. . .

Can the this pointer EVER point to a type different from the class that
contains the member function that the this pointer is being used in?
That is, is the type of the this pointer always determined entirely
syntactically (and never dynamically)?

Example: if a member function is invoked on an object of class Apple
(appleobj.drip_ it() ), and that function uses the scope operator to
call a function in the superclass Fruit (Fruit::drip_it () ), does the
keyword this in Fruit::drip_it( ) point to a Fruit object or an Apple
object?

There are two reasons that I am bothering to ask this question when I
am pretty sure I know the answer. (The answer is that this is always
statically bound, its type determined entirely syntactically.) The
first reason is that it would be cool if this were dynamically bound,
as it would allow derived-class functions calls on non-virtual methods,
which would probably screw things up royally but would also extend rtti
possibilities in intuitive ways.

The second reason I ask is that a number of resources on the topic are
ambiguous. They quote the C++ standard to the effect that this is
always of type K* when it appears in a function within the class K. But
they also say that this points to whatever object "the function is
invoked on," which isn't entirely clear but seems to leave open the
possibility that a function could be invoked on an object in a
subclass.

Addendum: Given the above situation--where an Apple object calls a
Fruit method--how would you from within the Fruit method
Fruit::drip_it( ) call another Fruit method that was virtual and
overridden in Apple? If, within Fruit::drip_it( ) you call
Fruit::suck_it( ) (defined virtual), will the compiler create code that
uses the lookup table for the this object (in this case, an Apple
object), or will it use the scope operator and call the suck_it()
routine within Fruit?

Thanks for your consideration. Regards,

**** *** ****
A d e n
**** *** ****

Jul 31 '05 #1
9 2179
* ad**@who.net:


The static type of 'this' is statically determined.

The dynamic type of 'this' is dynamically determined.

Cheerio,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 31 '05 #2
Thank you for your succinct and appropriate response.

But wouldn't it make sense/be useful for "this" to "track" the dynamic
type of its object? Then, there would be a (useful) difference between
this->do_it() and just do_it(), since the "this" version would call a
method in the (dynamic) class type of the "this" object (which might be
a subclass of the current class), while the non-this version would just
call the current class's method. Maybe I'm itching for a language in
which all methods are virtual, but this seems like a worthwhile
consideration.

The advantage of such an implementation would be that "this" would
cease to be a relatively empty keyword and would serve more of a
purpose. Further, it would conform in an interesting way to its
standard English usage, where it refers to an object in all of its
particularity and not just to an object as determined by its context.

What would be the disadvantage? More look-up tables, I suppose, and
hence inefficiency at run-time.

Cheers,

**** *** ****
A d e n
**** *** ****

Jul 31 '05 #3
ad**@who.net wrote:
Can the this pointer EVER point to a type different from the class that
contains the member function that the this pointer is being used in?
That is, is the type of the this pointer always determined entirely
syntactically (and never dynamically)?

Example: if a member function is invoked on an object of class Apple
(appleobj.drip_ it() ), and that function uses the scope operator to
call a function in the superclass Fruit (Fruit::drip_it () ), does the
keyword this in Fruit::drip_it( ) point to a Fruit object or an Apple
object?


You seem to mix two concepts: the type of the pointer and the type of the
thing it points to.

The type of the pointer at compile time determines which member functions is
used. If the member functions is virtual, the result is to use the thing
pointed to, by using a virtual function table or whatever other way the
compiler uses to implement virtual functions, to see at runtime where is
the function to be used.

And in case the funtion used is in a derived class, this points to an object
of this class, but of course in that case this class is the class of this.

--
Salu2
Jul 31 '05 #4
ad**@who.net wrote:
Thank you for your succinct and appropriate response.

But wouldn't it make sense/be useful for "this" to "track" the dynamic
type of its object? Then, there would be a (useful) difference between
this->do_it() and just do_it(), since the "this" version would call a
method in the (dynamic) class type of the "this" object (which might be
a subclass of the current class), while the non-this version would just
call the current class's method. Maybe I'm itching for a language in
which all methods are virtual, but this seems like a worthwhile
consideration.
If you want to decorate your code with this-> everywhere, feel free. But
please don't try to impose that verbosity on everyone else. <g>

To call the version of a function defined in your class use
ClassName::do_i t().

The advantage of such an implementation would be that "this" would
cease to be a relatively empty keyword and would serve more of a
purpose.


I'm sure 'this' isn't feeling underutilized. There's no need to give it
more to do.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 31 '05 #5
ad**@who.net wrote:
The advantage of such an implementation would be that "this" would
cease to be a relatively empty keyword and would serve more of a
purpose. Further, it would conform in an interesting way to its
standard English usage, where it refers to an object in all of its
particularity and not just to an object as determined by its context.
What would be the disadvantage? More look-up tables, I suppose, and
hence inefficiency at run-time.


The disadvantage is that this way is not an implementation of C++, is a
different language.

--
Salu2
Jul 31 '05 #6
If you want to call the statically typed method, just add
"<static_class_ name>::" in front of the method call. Most of the time
you mean to call the dynamic one, so this doesn't come up very often.

Aug 1 '05 #7
James,

This confuses me again when I thought it was clear.

If a Circle object calls a method of class Shape, "Shape::prep(), " and
there is a function call in Shape.prep() that uses "this," (implicitly
or explicitly, "this->draw()"), then the static-typed method will be
called, (right?), namely, the method Shape.draw(). (Unless Shape.draw()
is virtual, in which case the dynamic reference gets used.) When
calling a non-virtual method, there is no need to use
<static_class_n ame>:: with the implicit "this" in the method call. It
will resolve statically anyway. No?

**** *** ****
A d e n
**** *** ****

Aug 1 '05 #8
* ad**@who.net:

This confuses me again when I thought it was clear.


Adding 'this->' to the left of a member function call doesn't do anything
(unless the call is ambigious and the 'this->' happens to disambiguate).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Aug 1 '05 #9
Oh, I see what you are saying. So you want this->methodname() to call
the right method even if the method is not virtual? Well, that would
kind of not do what this-> is supposed to do and would very much break
backwards compatibility. I'm also not sure that it is useful: if you
want a virtual method, just make it virtual.

Aug 14 '05 #10

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

Similar topics

5
2780
by: Michael Stevens | last post by:
Probably the wrong wording but since I'm not a scripter I won't claim to know what I'm talking about. I got this script from www.htmlgoodies.com <script language="JavaScript"> <!-- window.open ('photos01.html','photogallery',config='height=550, width=750,toolbar=no, menubar=no, scrollbars=no, resizable=no, location=no, directories=no, status=no');
32
3179
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) {}
6
1577
by: Anders Borum | last post by:
Hello! I'm wondering what the recommendations are for using the "this" keyword? The problem is that I really like to use the "this" keyword, but it tends to make the code more verbose (which some don't like). Your oppinion please? ... internal void PageDeleted(Page page) {
2
1559
by: Bryan | last post by:
Hello all, Can anyone explain when one should use the "document" object and when one should use the "this" object? Also, is the "self" object the same as the "document" or "this" object?
0
1803
by: WORKING IN FAITH | last post by:
three years I LOVE You Monica More options 1 message - Collapse all WORKING IN FAITH View profile More options Nov 13, 11:29 am three years I LOVE You Monica
2
6859
NewYorker
by: NewYorker | last post by:
Write a switch statement that tests the value of the char variable response and performs the following actions: if response is y , the message Your request is being processed is printed if response is n , the message Thank you anyway for your consideration is printed if response is h , the message Sorry, no help is currently available is printed for any other value of response , the message Invalid entry; please try again is...
10
4794
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:
8
4544
by: =?Utf-8?B?VHJlY2l1cw==?= | last post by:
Hello, Newsgroupians: I have a large class with a lot of member variables. I also have a function in the class that I would like to change ALL Of the member variables. I am trying to assign "this" to the result, but I always get the error message, "Cannot assign to '<this>' because it is read-only." I've been searching on the Internet, and I notice some C# code is violating this rule. Perhaps their code is wrong.
5
6485
by: anEchteTrilingue | last post by:
Hi everybody. Thank you for reading my post. I am having trouble getting "this" to work in all versions of IE (it's fine in Firefox, opera, konqueror, etc). What I would like to do is add an event listener to an element to change its border on mouseover and mouseout. I don't want to use CSS to do this (long story). The problem is that "this" does not work at all in IE for me. I tried to do a try/catch statement and use...
0
8619
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8555
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 synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9112
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8817
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6484
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4336
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4571
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2994
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2258
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.