473,770 Members | 6,322 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why "this" is a pointer, I just realized!

You can overload the address-of operator. Consider a simple class:

class Blah
{
public:

Blah* operator&()
{
return this;
}
};

Now... consider if "this" was a reference (as before I've argued it should
be):

class Blah
{
public:

Blah* operator&()
{
return &this;
}
};
looks like an infinite loop to me...
Although to be honest I would've prefered "p_this".
-JKop
Jul 22 '05 #1
5 1383
"JKop" <NU**@NULL.NULL > wrote in message
news:Ss******** **********@news .indigo.ie...
You can overload the address-of operator. Consider a simple class:

class Blah
{
public:

Blah* operator&()
{
return this;
}
};

Now... consider if "this" was a reference (as before I've argued it should
be):

class Blah
{
public:

Blah* operator&()
{
return &this;
}
};
looks like an infinite loop to me...
Although to be honest I would've prefered "p_this".


In "The Design and Evolution of C++", Bjarne Stroustrup says that "this" is
a pointer and not a reference because references were not present in "C with
Classes" at the time that "this" was introduced.

--
David Hilsee
Jul 22 '05 #2
"David Hilsee" <da************ *@yahoo.com> wrote in message
news:25******** ************@co mcast.com...
"JKop" <NU**@NULL.NULL > wrote in message
news:Ss******** **********@news .indigo.ie...
You can overload the address-of operator. Consider a simple class:

class Blah
{
public:

Blah* operator&()
{
return this;
}
};

Now... consider if "this" was a reference (as before I've argued it should be):

class Blah
{
public:

Blah* operator&()
{
return &this;
}
};
looks like an infinite loop to me...
Although to be honest I would've prefered "p_this".
In "The Design and Evolution of C++", Bjarne Stroustrup says that "this"

is a pointer and not a reference because references were not present in "C with Classes" at the time that "this" was introduced.


I did a quick search and found that many people already said this the first
time you brought this up. I must have missed the discussion the first time
around. I'll add something new by pointing out that your overload of
operator& has little bearing on the matter, because it is rarely (if ever)
used in the way that you are using it. Normally, operator& is overloaded to
_alter_ the return value. If you just want to return "this", then you don't
need to overload operator&.

If you really wanted the address and wanted to avoid invoking operator&, you
could use something like boost's addressof
(http://www.boost.org/libs/utility/ut...htm#addressof), so it wouldn't
matter if "this" was a reference.

--
David Hilsee
Jul 22 '05 #3
JKop wrote:
You can overload the address-of operator. Consider a simple class:

class Blah
{
public:

Blah* operator&()
{
return this;
}
};

Now... consider if "this" was a reference (as before I've argued it should
be):

class Blah
{
public:

Blah* operator&()
{
return &this;
}
};
looks like an infinite loop to me...
Although to be honest I would've prefered "p_this".


Provided the reference to "Design and Evolution" that David provided, I
want to note that C++ is not a language of the lab, but a product of
evolution that *addresses real world problems*.
So here is the evolution of C++:
BCPL
B: As dmr mentions, "B can be thought of as C without types; more
accurately, it is BCPL squeezed into 8K bytes of memory and filtered
through Thompson's brain."

http://www.cs.bell-labs.com/who/dmr/chist.html
C90 (ISO C90)
C++98 (some concepts also taken from Simula).


Regards,

Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #4
Ioannis Vranos wrote:
Provided the reference to "Design and Evolution" that David provided, I
want to note that C++ is not a language of the lab, but a product of
evolution that *addresses real world problems*.


Notice Ioannis is saying that C++ was deployed to business users before all
its logical inconsistencies were removed. These tend to shock newbies who
think that a language designer must be somehow infallible.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces
Jul 22 '05 #5
"Phlip" <ph*******@yaho o.com> wrote in message news:<8c******* ***********@new ssvr33.news.pro digy.com>...

[ ... ]
Notice Ioannis is saying that C++ was deployed to business users before all
its logical inconsistencies were removed. These tend to shock newbies who
think that a language designer must be somehow infallible.


Regardless of the method by which it was developed, I've yet to see a
single language from which all logical inconsistencies had been
removed, or which lacked "features" that could surprise newbies. In
the end, language designers are human, and the languages they design
reflect that.

OTOH, I'd keep in mind that a language is basically a specification
for some software. Most current thought on specs for software seems to
emphasize collecting information from real users on a regular and
ongoing basis throughout the design process, and IMO, a language is no
different. If anything, I'd say that languages generally suffer more
from language designers who are too close to infallible. They design
languages that work well for their aims, but they're enough different
from the average programmer that they often design things that average
programmers can barely understand, not to mention really use.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #6

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

Similar topics

14
2263
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;
32
3207
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) {}
9
2184
by: aden | last post by:
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() ),...
3
1277
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); }
8
2238
by: solarin | last post by:
Hi all. I'm writting a logger class to write all the debug/info/warning/error messages in a file. Every time a class needs to send any message, should send a code (int) and a message (string). I would like to write also in the file, the class that has sent the message. all the clases that need to send a message, derives from a base clas: class I_want_To_Send : public I_will_write_In_Logger
4
4171
by: craig | last post by:
During construction of an object "parent", if you create a subobject that stores a pointer to the parent (through the "this" pointer), will that pointer be valid when the subobject is later called? class Parent { Parent::Parent() { child = new Child( this) }; Child *child; };
10
2743
by: Angel Tsankov | last post by:
Hello! Is the following code illformed or does it yield undefined behaviour: class a {}; class b {
3
1597
by: Martin Drautzburg | last post by:
For reasons related to python/swig and garbage collecting I want to remember every object in a linked list. I have a common base class RCObj which in turn has a static std::list<RCObj*memory. My problem is that I cannot get RCObj store the true this pointer. Even if I pass it explicitly from a derived class (called "Part") as in: Part::Part() { this->RCObj::init(this);
6
2328
by: babakandme | last post by:
Hi to every body...:D I'm a novice C++ programmer & I've a question, I have the ClassA & in it's constructor, I instantiate ClassB, and I want send "this" pointer """pointer to ClassA""" to the ClassB. But I get this Error from the compiler: and it's in ClassB... Error from the compiler: error C2061: syntax error : identifier 'TestA' error C2143: syntax error : missing ';' before '*'
2
1451
by: Sergei Shelukhin | last post by:
Hi. I have barely written any C++ for past 3-4 years, I need to refresh it in my memory so I decided to do some coding starting with classic algos and data structures, using Visual Studio 9, unmanaged code. Everything is humming, but I have the following problem I cannot figure out. Say we have the following: template <typename Tclass SLL { public: SLL<T>* AddHead(T value);
0
9591
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
9425
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
10225
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
10053
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7415
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
5312
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
5449
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3969
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
3
2816
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.