Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why "this" is a pointer, I just realized!

Reply
Thread Tools

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

 
 
JKop
Guest
Posts: n/a
 
      09-06-2004
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
 
Reply With Quote
 
 
 
 
David Hilsee
Guest
Posts: n/a
 
      09-06-2004
"JKop" <> wrote in message
news:Ss5%c.26732$...
> 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


 
Reply With Quote
 
 
 
 
David Hilsee
Guest
Posts: n/a
 
      09-07-2004
"David Hilsee" <> wrote in message
news:25KdnR1CoaexbKHcRVn-...
> "JKop" <> wrote in message
> news:Ss5%c.26732$...
> > 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


 
Reply With Quote
 
Ioannis Vranos
Guest
Posts: n/a
 
      09-07-2004
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
 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      09-07-2004
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


 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      09-07-2004
"Phlip" <> wrote in message news:<8cb%c.14831$. 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
allow you to move just about any music to your iPud, MP3 player , or just burn it to disk sbcmynews Computer Support 4 05-01-2005 03:53 PM
Why use a JavaBean? Why not just classes in a jar file? Tom Dyess Java 2 01-20-2005 09:52 AM
Just bought a Digital Rebel just 1 question RacerX Digital Photography 7 11-21-2003 01:51 PM



Advertisments