this

J

JKop

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

-JKop
 
P

Pete C.

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

-JKop

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
 
A

Alf P. Steinbach

* JKop:
Why did they make "this" a pointer and not a reference?

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.

the hell!! It's so stupid!

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

Chris Gordon-Smith

Pete said:
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

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..."
 
D

David Harmon

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

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.
 
P

Phlip

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

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.
 
I

Ioannis Vranos

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

-JKop


"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
 
J

JKop

Ioannis Vranos posted:
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.

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
 
I

Ioannis Vranos

JKop said:
Ioannis Vranos posted:




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!



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






Regards,

Ioannis Vranos
 
R

Roman Ziak

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

-JKop

.... 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 ?
 
P

Phlip

Ioannis said:
void OkButton_clicked(Object *pSender, EventArgs *Args)

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.
 
R

Roman Ziak

Roman Ziak said:
... 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 ?

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
 
P

Pete C.

Phlip said:
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.

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
 
I

Ioannis Vranos

Phlip said:
Ioannis Vranos wrote:




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


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
 
C

Cy Edmunds

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

-JKop

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.
 
J

Julie

Cy said:
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.

Is C++ an open standard? If so, then it is not beyond his/our control...
 
J

JKop

Cy Edmunds posted:
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.


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
 
J

John Harrison

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?

Does that question need an answer?

john
 
J

JKop

Roman Ziak posted:
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.


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,171
Messages
2,570,935
Members
47,472
Latest member
KarissaBor

Latest Threads

Top