Connecting Tech Pros Worldwide Help | Site Map

this

JKop
Guest
 
Posts: n/a
#1: Jul 22 '05

Why the hell did they make "this" a pointer and not a reference?!! It's so
stupid!

-JKop
Pete C.
Guest
 
Posts: n/a
#2: Jul 22 '05

re: this


JKop wrote:[color=blue]
> Why the hell did they make "this" a pointer and not a reference?!!
> It's so stupid!
>
> -JKop[/color]

Perhaps references were not added to the language when the this pointer
was - I assume `this' was added in the first version.
Besides, it's easy enough to use *this.

- Pete


Alf P. Steinbach
Guest
 
Posts: n/a
#3: Jul 22 '05

re: this


* JKop:[color=blue]
>
> Why did they make "this" a pointer and not a reference?[/color]

I believe it was because the language was created without references,
originally as as a C preprocessor, and references only added to the
language much later when it had already become an established language
on its own. But that's only what I believe, I do not really _know_.
Another possible reason is that (and this I do know) before
standardization, where other mechanisms were introduced, you could take
charge of allocation by assigning to the 'this' pointer in a
constructor, and in the corresponding destructor; it's not allowed now.

[color=blue]
> the hell!! It's so stupid![/color]

"Multiple exclamation marks," he went on, shaking his head, "are a sure
sign of a diseased mind." -- Terry Pratchett

--
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?
Chris Gordon-Smith
Guest
 
Posts: n/a
#4: Jul 22 '05

re: this


Pete C. wrote:
[color=blue]
> JKop wrote:[color=green]
>> Why the hell did they make "this" a pointer and not a reference?!!
>> It's so stupid!
>>
>> -JKop[/color]
>
> Perhaps references were not added to the language when the this pointer
> was - I assume `this' was added in the first version.
> Besides, it's easy enough to use *this.
>
> - Pete[/color]

Looks like the above is correct. Bjarne Stroustrup's book "The Design and
Evolution of C++" says

"Sometimes people ask why this is a pointer rather than a reference ...
When this was introduced into C with Classes. the language didn't have
references..."
--
Chris Gordon-Smith
London
Homepage: http://graffiti.virgin.net/c.gordon-smith/
Email Address: Please see my Home Page
David Harmon
Guest
 
Posts: n/a
#5: Jul 22 '05

re: this


On Mon, 21 Jun 2004 18:27:33 GMT in comp.lang.c++, JKop <NULL@NULL.NULL>
wrote,[color=blue]
>
>Why the hell did they make "this" a pointer and not a reference?!! It's so
>stupid![/color]

So sorry. 'this' was invented before references were, and it was too
late to go back. Everyone knows now that a reference would have been
better.

Phlip
Guest
 
Posts: n/a
#6: Jul 22 '05

re: this


JKop wrote:
[color=blue]
> Why the hell did they make "this" a pointer and not a reference?!! It's so
> stupid![/color]

Because it was invented first.

'this' has all the characteristics of a reference (cannot re-seat, cannot
refer at NULL, has no address, etc). If references had been invented first,
this would have been one.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


Ioannis Vranos
Guest
 
Posts: n/a
#7: Jul 22 '05

re: this


JKop wrote:[color=blue]
> Why the hell did they make "this" a pointer and not a reference?!! It's so
> stupid!
>
> -JKop[/color]


"Stupid" is a strong word. Many times when I create event handlers
(using system extensions) in my platform, I pass the object containing
the event handler and the event handler (the member function). So for
example:

class myform: public Form
{
// ...
public:
void OkButton_clicked(Object *pSender, EventArgs *Args)
{
// ...
}
// ..

myform()
{
// ...
// I register the member function above as an event handler
button->Click+=new EventHandler(this, &form::OkButton_clicked);

// ...
}
// ...
};



Had this been a reference, passing "&this" above instead of "this",
would be tedious and in the event handling mechanism used in the
platform pointers to objects get passed (for example I could pass a
pointer of another object type).


So I guess everything depends on context. Your "clever" approach, would
be "stupid" in my platform.






Regards,

Ioannis Vranos
JKop
Guest
 
Posts: n/a
#8: Jul 22 '05

re: this


Ioannis Vranos posted:
[color=blue]
> button->Click+=new EventHandler(this, &form::OkButton_clicked);
>
> Had this been a reference, passing "&this" above instead of "this",
> would be tedious and in the event handling mechanism used in the
> platform pointers to objects get passed (for example I could pass a
> pointer of another object type).
>
>
> So I guess everything depends on context. Your "clever" approach, would
> be "stupid" in my platform.[/color]

Let's just say it's a matter of opinion!

Here's how I look at it:


Use pointers when there's arrays involved, eg. a strlen function.

Use pointers when you've to re-seat the pointer for any reason.

Use references everywhere else!


-JKop
Ioannis Vranos
Guest
 
Posts: n/a
#9: Jul 22 '05

re: this


JKop wrote:
[color=blue]
> Ioannis Vranos posted:
>
>[color=green]
>> button->Click+=new EventHandler(this, &form::OkButton_clicked);
>>
>>Had this been a reference, passing "&this" above instead of "this",
>>would be tedious and in the event handling mechanism used in the
>>platform pointers to objects get passed (for example I could pass a
>>pointer of another object type).
>>
>>
>>So I guess everything depends on context. Your "clever" approach, would
>>be "stupid" in my platform.[/color]
>
>
> Let's just say it's a matter of opinion!
>
> Here's how I look at it:
>
>
> Use pointers when there's arrays involved, eg. a strlen function.
>
> Use pointers when you've to re-seat the pointer for any reason.
>
> Use references everywhere else![/color]



That's your style. The general rule is use pointers or references when
you have to pass something "by reference".






Regards,

Ioannis Vranos
Phlip
Guest
 
Posts: n/a
#10: Jul 22 '05

re: this


Ioannis Vranos wrote:
[color=blue]
> void OkButton_clicked(Object *pSender, EventArgs *Args)[/color]

Why are these pointers? At a method interface, the only reason to pass a
pointer is it might be NULL.

Both those objects must truly exist. Prefer references unless you need a
pointer's extra abilities.

--
Phlip
http://industrialxp.org/community/bi...UserInterfaces


Pete C.
Guest
 
Posts: n/a
#11: Jul 22 '05

re: this


Phlip wrote:[color=blue]
> Ioannis Vranos wrote:
>[color=green]
>> void OkButton_clicked(Object *pSender, EventArgs *Args)[/color]
>
> Why are these pointers? At a method interface, the only reason to
> pass a pointer is it might be NULL.
>
> Both those objects must truly exist. Prefer references unless you
> need a pointer's extra abilities.[/color]

It is able to be null - he's using System::Windows::Forms from the .NET
framework. Though I must admit I don't think that's a good design decision.

- Pete


Roman Ziak
Guest
 
Posts: n/a
#12: Jul 22 '05

re: this


"JKop" <NULL@NULL.NULL> wrote in message
news:pMFBc.2800$Z14.3374@news.indigo.ie...[color=blue]
>
> Why the hell did they make "this" a pointer and not a reference?!! It's so
> stupid!
>
> -JKop[/color]

.... one can achieve the same results with both. Just different operators.

Do you prefer '->' before '.' ? Because you will type one character less ?

Can somebody explain to me why reference is considered to be _better_ than
pointer ? Is that because one can overload operator '.' and not '->' ? Or
something else ?


Ioannis Vranos
Guest
 
Posts: n/a
#13: Jul 22 '05

re: this


Phlip wrote:
[color=blue]
> Ioannis Vranos wrote:
>
>[color=green]
>> void OkButton_clicked(Object *pSender, EventArgs *Args)[/color]
>
>
> Why are these pointers? At a method interface, the only reason to pass a
> pointer is it might be NULL.[/color]


Because this is how the platform works.


button->Click+=new EventHandler(this, &form::OkButton_clicked);


The EventHandler constructor gets pointers, and the OkButton_clicked
event handler must have a signature accepting those pointer types above.






Regards,

Ioannis Vranos
Roman Ziak
Guest
 
Posts: n/a
#14: Jul 22 '05

re: this


"Roman Ziak" <roman@nospam.com> wrote in message
news:xEHBc.5792$Nz.562661@news20.bellglobal.com...[color=blue]
>
> ... one can achieve the same results with both. Just different operators.
>
> Do you prefer '->' before '.' ? Because you will type one character less ?
>
> Can somebody explain to me why reference is considered to be _better_ than
> pointer ? Is that because one can overload operator '.' and not '->' ? Or
> something else ?
>[/color]

I see. Phlip (in this thread couple posts above) explained difference
between references and pointers.

Well, from that prospective, it is better for _this_ to be reference for the
consistency sake as it has all attributes of reference (although I am not
sure about NULL, why one cannot assign NULL to _this_ ?)

However, I would still like to hear a reason about references vs. pointers.
It apears to me that references are limited versions of pointers.

Roman


Cy Edmunds
Guest
 
Posts: n/a
#15: Jul 22 '05

re: this


"JKop" <NULL@NULL.NULL> wrote in message
news:pMFBc.2800$Z14.3374@news.indigo.ie...[color=blue]
>
> Why the hell did they make "this" a pointer and not a reference?!! It's so
> stupid!
>
> -JKop[/color]

I've got an idea: why don't you learn the language as it is? What's stupid
is passing judgment on things beyond your control.

--
Cy
http://home.rochester.rr.com/cyhome/


Julie
Guest
 
Posts: n/a
#16: Jul 22 '05

re: this


Cy Edmunds wrote:[color=blue]
>
> "JKop" <NULL@NULL.NULL> wrote in message
> news:pMFBc.2800$Z14.3374@news.indigo.ie...[color=green]
> >
> > Why the hell did they make "this" a pointer and not a reference?!! It's so
> > stupid!
> >
> > -JKop[/color]
>
> I've got an idea: why don't you learn the language as it is? What's stupid
> is passing judgment on things beyond your control.
>
> --
> Cy
> http://home.rochester.rr.com/cyhome/[/color]

Is C++ an open standard? If so, then it is not beyond his/our control...
JKop
Guest
 
Posts: n/a
#17: Jul 22 '05

re: this


Cy Edmunds posted:
[color=blue]
> "JKop" <NULL@NULL.NULL> wrote in message
> news:pMFBc.2800$Z14.3374@news.indigo.ie...[color=green]
>>
>> Why the hell did they make "this" a pointer and not a reference?!!
>> It's so stupid!
>>
>> -JKop[/color]
>
> I've got an idea: why don't you learn the language as it is? What's
> stupid is passing judgment on things beyond your control.[/color]


I'm going to ring up The Oxford Dictionary company and supply them with a
new definition for "fascist".

From my perspective, passing judgement on things that are allegedly beyond
my control is not at all stupid, it's fun, I enjoy it. Is bungee jumping
stupid?


-JKop
John Harrison
Guest
 
Posts: n/a
#18: Jul 22 '05

re: this


>[color=blue]
> From my perspective, passing judgement on things that are allegedly beyond
> my control is not at all stupid, it's fun, I enjoy it. Is bungee jumping
> stupid?
>[/color]

Does that question need an answer?

john


JKop
Guest
 
Posts: n/a
#19: Jul 22 '05

re: this


Roman Ziak posted:
[color=blue]
> Well, from that prospective, it is better for _this_ to be reference
> for the consistency sake as it has all attributes of reference
> (although I am not sure about NULL, why one cannot assign NULL to
> _this_ ?)
>
> However, I would still like to hear a reason about references vs.
> pointers. It apears to me that references are limited versions of
> pointers.[/color]


I'm really starting to believe that people should learn about references
long before they ever learn about pointers.

First things first. Why do you use a reference? So that a function can alter
the variable supplied to it:

#include <iostream>

void ChangeArg(int& p)
{
p += 4;
}

int main(void)
{
int s = 7;

ChangeArg(s);

if (s != 11)
{
std::cout << "Burn this alleged C++ compiler";
}
}


And that should do them grand for a while!

Then, only when they get to playing with arrays, do they need to hear about
pointers:

unsigned char GetStringLength(const char* pString)
{
unsigned char count = 0;

while (pString++)
{
++count;
}

return count;
}


Or even this:

unsigned char GetStringLength(const char& first_char)
{
const char* pString = &first_char;

unsigned char count = 0;

while (pString++)
{
++count;
}

return count;
}

or even this:

unsigned char GetStringLength(const char& first_char)
{
unsigned char count = 0;

while ( (&first_char)[count++] ) { }

return count;
}


After that, there's places where you may have to re-seat a pointer. Consider
that you're an employer:

class Employer
{
public:

Employee* pStoreManager;
};


You may not always have the same store manager, he may get killed in a car
crash on the way to work and you'll have to hire some-one else:

Employer me;

Employee original_manager;

me.pStoreManager = &original_manager;

//Manager gets killed

Employee new_manager;

Employer.pStoreManage = &new_manager;



Once you've come to "accept" that, you can do into the realms of:

A) Binding a temporary to a reference, thus avoiding an unecessary copy

B) Returning a reference from a function


A:

int cheese(void)
{
return 12;
}

void chalk(int poo)
{
poo;
}

int main(void)
{
const int& grass = cheese();

//Time goes by

chalk(grass);
}


B:


#include <iostream>


int g_Ramp = 42;


int& GiveRamp(void)
{
return g_Ramp;
}

int main(void)
{
GiveRamp() = 72;

int& road = GiveRamp();

if ( (&road != &g_Ramp) || (g_Ramp != 72) )
{
std::cout << "Burn this alleged C++ compiler";
}
}


Hope that helps!


-JKop

JKop
Guest
 
Posts: n/a
#20: Jul 22 '05

re: this


John Harrison posted:
[color=blue][color=green]
>> From my perspective, passing judgement on things that are allegedly
>> beyond my control is not at all stupid, it's fun, I enjoy it. Is
>> bungee jumping stupid?
>>[/color]
>
> Does that question need an answer?
>
> john[/color]


Yes, yes it does.


-JKop
Fraser Ross
Guest
 
Posts: n/a
#21: Jul 22 '05

re: this


How difficult would it be to add a keyword such as thisref to use as an
alternative to this? Or alternatively this could be renamed thisptr and the
keyword this becomes a reference type. It does not look difficult to
introduce.

Fraser.


JKop
Guest
 
Posts: n/a
#22: Jul 22 '05

re: this


Fraser Ross posted:
[color=blue]
> How difficult would it be to add a keyword such as thisref to use as an
> alternative to this? Or alternatively this could be renamed thisptr
> and the keyword this becomes a reference type. It does not look
> difficult to introduce.
>
> Fraser.[/color]

Well, the dye has already been cast. We can't just get rid of "this" and
render good code bad. Unless... with each copy of the Standard, a new
program is released that will convert CPP files, eg. this will become
thisptr.

I myself would have prefered:

this as a reference

&this as a pointer

or maybe even:

pThis or p_this as a pointer.


Now that that's out of the question, thisref would be a good idea. The thing
is would anyone be bothered at this stage!

-JKop
Pete C.
Guest
 
Posts: n/a
#23: Jul 22 '05

re: this


Julie wrote:[color=blue]
> Cy Edmunds wrote:[color=green]
>>
>> "JKop" <NULL@NULL.NULL> wrote in message
>> news:pMFBc.2800$Z14.3374@news.indigo.ie...[color=darkred]
>>>
>>> Why the hell did they make "this" a pointer and not a reference?!!
>>> It's so stupid!
>>>
>>> -JKop[/color]
>>
>> I've got an idea: why don't you learn the language as it is? What's
>> stupid is passing judgment on things beyond your control.
>>
>> --
>> Cy
>> http://home.rochester.rr.com/cyhome/[/color]
>
> Is C++ an open standard? If so, then it is not beyond his/our
> control...[/color]

No, it's not.

- Pete


Ioannis Vranos
Guest
 
Posts: n/a
#24: Jul 22 '05

re: this


Roman Ziak wrote:
[color=blue]
> "JKop" <NULL@NULL.NULL> wrote in message
> news:pMFBc.2800$Z14.3374@news.indigo.ie...
>[color=green]
>>Why the hell did they make "this" a pointer and not a reference?!! It's so
>>stupid!
>>
>>-JKop[/color]
>
>
> .... one can achieve the same results with both. Just different operators.
>
> Do you prefer '->' before '.' ? Because you will type one character less ?
>
> Can somebody explain to me why reference is considered to be _better_ than
> pointer ? Is that because one can overload operator '.' and not '->' ? Or
> something else ?[/color]



Who said it is better? Everything *depends* on context of use. The
difference between references and pointers is that references do not
have an address of their own and we can not assume that they occupy
space since in many cases they are optimised out by the compiler completely.






Regards,

Ioannis Vranos
Ioannis Vranos
Guest
 
Posts: n/a
#25: Jul 22 '05

re: this


John Harrison wrote:
[color=blue][color=green]
>>From my perspective, passing judgement on things that are allegedly beyond
>>my control is not at all stupid, it's fun, I enjoy it. Is bungee jumping
>>stupid?
>>[/color]
>
>
> Does that question need an answer?[/color]


Eheheh. :-)






Regards,

Ioannis Vranos
Ioannis Vranos
Guest
 
Posts: n/a
#26: Jul 22 '05

re: this


JKop wrote:
[color=blue]
> Fraser Ross posted:
>
>[color=green]
>>How difficult would it be to add a keyword such as thisref to use as an
>>alternative to this? Or alternatively this could be renamed thisptr
>>and the keyword this becomes a reference type. It does not look
>>difficult to introduce.
>>
>>Fraser.[/color]
>
>
> Well, the dye has already been cast. We can't just get rid of "this" and
> render good code bad. Unless... with each copy of the Standard, a new
> program is released that will convert CPP files, eg. this will become
> thisptr.
>
> I myself would have prefered:
>
> this as a reference
>
> &this as a pointer
>
> or maybe even:
>
> pThis or p_this as a pointer.
>
>
> Now that that's out of the question, thisref would be a good idea. The thing
> is would anyone be bothered at this stage![/color]


The general approach of the Standard's committee is to be as
conservative as possible introducing only badly needed features. And
this is not a badly needed feature.






Regards,

Ioannis Vranos
Markus Dehmann
Guest
 
Posts: n/a
#27: Jul 22 '05

re: this


JKop <NULL@NULL.NULL> wrote in message news:<pMFBc.2800$Z14.3374@news.indigo.ie>...[color=blue]
> Why the hell did they make "this" a pointer and not a reference?!! It's so
> stupid!
>
> -JKop[/color]

Define this:
#define this_ref (*this)

Now you can use this_ref.
Closed Thread