473,387 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1355
"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********************@comcast.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*******@yahoo.com> wrote in message news:<8c******************@newssvr33.news.prodigy. 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
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...
32
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
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...
3
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...
8
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)....
4
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?...
10
by: Angel Tsankov | last post by:
Hello! Is the following code illformed or does it yield undefined behaviour: class a {}; class b {
3
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...
6
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...
2
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,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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
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,...

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.