Topic: this' as reference
Author: David R Tribble <dtribble@technologist.com>
Date: 1999/05/26 Raw View
petebecker@acm.org wrote:
>
> >But we could allow the '.' and '->' operators to be
> >used interchangeably with 'this' operand
> >without breaking any existing code.
>
> Thus bringing in, through the back door, the ability to overload
> operator dot.
Eesh. Forget I brought it up.
OTOH, we could allow operator->() to be overloaded, but not
operator.(), and simply allow users to use either one (according
to the API intentions of the class designer.) I really don't see
the advantage of having to distinguish between "pointers use ->"
and "objects use ." in 90% of my code; I think the distinction
is artificial. It's also a pain in those rare instances where I
have to convert an object (or reference) into a pointer type, since
I have to change all the '.' operators to '->'. Why not simply let
the compiler deal with it, since it knows whether the left operand
is a pointer or not?
FWIW, I also think that 'a.b' is easier to read than 'a->b', but
aesthetics shouldn't have too much of an impact on language design.
(Did I really say that?!) After all, I think 'a = b' looks nicer
than 'a == b', but I've gotten used to the latter.
-- David R. Tribble, dtribble@technologist.com --
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: petebecker@acm.org
Date: 1999/05/20 Raw View
>But we could allow the '.' and '->' operators to be
>used interchangeably with 'this' operand
>without breaking any existing code.
Thus bringing in, through the back door, the ability to overload operator
dot.
-- Pete
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Joerg Schaible" <Joerg.Schaible.A@T.gft.de>
Date: 1999/05/11 Raw View
Personally I use
#define self (*this)
but I would prever beeing self part of the language.
Greetings, Jvrg
--
BTW: It is normally better to answer to the group! For direct mail reply
exchange the ".A@T." by "@"
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@localhost.localdomain.COM (Siemel Naran)
Date: 1999/05/10 Raw View
On 09 May 99 17:51:41 GMT, James Kuyper <kuyper@wizard.net> wrote:
>Siemel Naran wrote:
>> > this = (T*) malloc (sizeof (*this));
>> In order for this statment to work, "this" must be a reference to a
>> pointer.
>
>Why must it be a reference to a pointer, rather than merely a pointer?
Because otherwise you'd be modifying a local. Eg,
T::T() { this=T(*)malloc(sizeof(T)); }
becomes
void T_ctor(T *const this) { this=T(*)malloc(sizeof(T)); } // memory leak!
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "Robert O'Dowd" <nospam@nonexistant.com>
Date: 1999/05/10 Raw View
David R Tribble wrote:
>
[Snip]
>
> But still, wouldn't it make as much sense for 'this' to be a
> reference as to be a pointer? After all, you can't modify the 'this'
> pointer, can you? And it would seem to add a certain symmetry to
> the language. For example:
>
> const Foo & Foo::Foo(const Foo &r)
> {
> #if existing_semantics
>
> if (this == &r) // A-1
This assumes that operator&() is not defined for class Foo.
I would really prefer to do
if (this == r.this)
....
> return *this;
>
> this->memb = r.memb; // A-2
> update(this); // A-3
>
> return *this; // A-4
>
> #elif new_semantics
>
> if (&this == &r) // B-1
> return this;
>
> this.memb = r.memb; // B-2
> update(&this); // B-3
>
> return this; // B-4
>
> #endif
> }
>
> Please don't tell me that we can't possibly change the language
> now, because I know that already. But we could allow the '.' and
> '->' operators to be used interchangeably with 'this' operand
> without breaking any existing code.
>
I have trouble with the notation of this being either a reference
or a pointer. Mainly because I'd have to scratch my head and work
out which is being used at any given time. I'll bet it's possible
to work out cases where the usage would be ambiguous.
If the reference semantics are really wanted, use a different
name, such as "object". In effect, doing something that works out
as a general purpose form of doing
#define object (*this)
-<Automagically included trailer>
Robert O'Dowd Ph +61 (8) 8259 6546
MOD/DSTO Fax +61 (8) 8259 5139
P.O. Box 1500 Email:
robert.odowd@dsto.defence.gov.au
Salisbury, South Australia, 5108
Disclaimer: Opinions above are mine and may be worth what you paid for
them
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Ron Natalie <ron@sensor.com>
Date: 1999/05/10 Raw View
Barry Margolin wrote:
>
> And
>
> delete this;
>
> wasn't unheard of, either.
>
And it's still not...
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/05/10 Raw View
Siemel Naran wrote:
>
> On 07 May 99 20:13:52 GMT, Valentin Bonnard <Bonnard.V@wanadoo.fr> wrote:
>
> > this = (T*) malloc (sizeof (*this));
>
> In order for this statment to work, "this" must be a reference to a
> pointer.
In other words it's a lvalue of pointer type, just as any
normal class member.
--
Valentin Bonnard
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: David R Tribble <dtribble@technologist.com>
Date: 1999/05/07 Raw View
David R Tribble <dtribble@technologist.com> writes
>> But still, wouldn't it make as much sense for 'this' to be a
>> reference as to be a pointer? After all, you can't modify the 'this'
>> pointer, can you? And it would seem to add a certain symmetry to
>> the language. ...
Francis Glassborow wrote:
> But once upon a time you could do exactly that (modify this).
I understand. But why would you want to? And did anyone, in fact,
do this?
(Methinks it was simply an oversight on the part of Stroustrup.)
-- David R. Tribble, dtribble@technologist.com --
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/05/07 Raw View
David R Tribble wrote:
>
> Francis Glassborow wrote:
> > But once upon a time you could do exactly that (modify this).
>
> I understand. But why would you want to? And did anyone, in fact,
> do this?
Yes, many people
> (Methinks it was simply an oversight on the part of Stroustrup.)
It wasn't. I was the way to allocate memory:
this = (T*) malloc (sizeof (*this));
or something like that.
--
Valentin Bonnard
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Valentin Bonnard <Bonnard.V@wanadoo.fr>
Date: 1999/05/07 Raw View
David R Tribble wrote:
> I suspect that the fact that 'this' is a pointer rather than a
> reference is probably the result of historical accident, in that
> references were probably added to the early C++ language long after
> the invention of 'this'.
Correct, 'this' is almost there since member functions (C with
classes ?).
> But still, wouldn't it make as much sense for 'this' to be a
> reference as to be a pointer?
Yes: it can't be null, it doesn't change, it isn't an object...
But there is no point in _changing_ that. Of couse, if
it was a new language... (but then a new language wouldn't
use the C++ syntax anyway)
--
Valentin Bonnard
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Barry Margolin <barmar@bbnplanet.com>
Date: 1999/05/07 Raw View
In article <37334917.2495@wanadoo.fr>,
Valentin Bonnard <Bonnard.V@wanadoo.fr> wrote:
>David R Tribble wrote:
>> (Methinks it was simply an oversight on the part of Stroustrup.)
>
>It wasn't. I was the way to allocate memory:
>
> this = (T*) malloc (sizeof (*this));
>
>or something like that.
And
delete this;
wasn't unheard of, either.
--
Barry Margolin, barmar@bbnplanet.com
GTE Internetworking, Powered by BBN, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: sbnaran@fermi.ceg.uiuc.edu (Siemel Naran)
Date: 1999/05/07 Raw View
On 07 May 99 20:13:52 GMT, Valentin Bonnard <Bonnard.V@wanadoo.fr> wrote:
> this = (T*) malloc (sizeof (*this));
In order for this statment to work, "this" must be a reference to a
pointer.
--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: stephen.clamage@sun.com (Steve Clamage)
Date: 1999/05/08 Raw View
David R Tribble <dtribble@technologist.com> writes:
>I suspect that the fact that 'this' is a pointer rather than a
>reference is probably the result of historical accident, in that
>references were probably added to the early C++ language long after
>the invention of 'this'.
Right.
>But still, wouldn't it make as much sense for 'this' to be a
>reference as to be a pointer?
Yes. If we were starting over, "this" would be a reference.
And if I had my way, it wouldn't be called "this", because
"this" is hard to talk about. And while we are redesigning the
entire language, I have a little list ...
>After all, you can't modify the 'this' pointer, can you?
Not any more. You used to be able to assign to "this" as a form
of class-specific memory management. When class-specific
operator new/delete was added to the language, assigning
to "this" was disallowed.
>Please don't tell me that we can't possibly change the language
>now, because I know that already. But we could allow the '.' and
>'->' operators to be used interchangeably with 'this' operand
>without breaking any existing code.
It would break "delete this". Maybe another special case? :-)
--
Steve Clamage, stephen.clamage@sun.com
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "John D. Hickin" <hickin@Hydro.CAM.ORG>
Date: 1999/05/08 Raw View
Siemel Naran wrote:
>
> On 07 May 99 20:13:52 GMT, Valentin Bonnard <Bonnard.V@wanadoo.fr> wrote:
>
> > this = (T*) malloc (sizeof (*this));
>
> In order for this statment to work, "this" must be a reference to a
> pointer.
It merely needs to have equivalent semantics. But there is a bit more to
it.
The rules stated that assignment of a non-0 value to this, worked in a
constructor only, and had to be to be done before the use of any member
of the class. Assigning a value of 0 in a destructor had the effect of
eliminating the normal deallocation; it also suppressed the implicit
calls of destructors for bases and members.
It wasn't very practical and it was deprecated by the time the ARM was
published; class operator new/delete had been added to the language by
then.
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: James Kuyper <kuyper@wizard.net>
Date: 1999/05/09 Raw View
Siemel Naran wrote:
>
> On 07 May 99 20:13:52 GMT, Valentin Bonnard <Bonnard.V@wanadoo.fr> wrote:
>
> > this = (T*) malloc (sizeof (*this));
>
> In order for this statment to work, "this" must be a reference to a
> pointer.
Why must it be a reference to a pointer, rather than merely a pointer?
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: "William M. Miller" <wmm@fastdial.net>
Date: 1999/05/09 Raw View
David R Tribble <dtribble@technologist.com> wrote in message
news:37334749.D00FA778@technologist.com...
> >> But still, wouldn't it make as much sense for 'this' to be a
> >> reference as to be a pointer? After all, you can't modify the 'this'
> >> pointer, can you? And it would seem to add a certain symmetry to
> >> the language. ...
>
> Francis Glassborow wrote:
> > But once upon a time you could do exactly that (modify this).
>
> I understand. But why would you want to? And did anyone, in fact,
> do this?
>
> (Methinks it was simply an oversight on the part of Stroustrup.)
No. In the earliest versions of C++, there was no "operator new". The
only way to take control of the allocation of objects was to assign to
"this." Furthermore, you had to be careful not to assign to "this" if
the constructor was being called for a static or automatic object
(which you could tell by whether the value of "this" was 0 or not), so
many constructors were written as:
C::C() {
if (!this) {
this = my_alloc();
}
// ...
}
The invention of "operator new" was a major step forward in the
development of the language (happened in '87 or '88, as I recall).
There's also history behind the answer to the original question.
Back when C++ was "C with classes," there were no references --
but there were member functions, so the only thing to do with
"this" was to make it a pointer. Bjarne has said publicly that
"this" would have been a reference if references had existed;
by the time references were invented, however, there was
already a body of code that would have been broken by the
change.
-- William M. Miller, Software Emancipation Technology, Inc.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: David R Tribble <dtribble@technologist.com>
Date: 1999/05/07 Raw View
I suspect that the fact that 'this' is a pointer rather than a
reference is probably the result of historical accident, in that
references were probably added to the early C++ language long after
the invention of 'this'.
But still, wouldn't it make as much sense for 'this' to be a
reference as to be a pointer? After all, you can't modify the 'this'
pointer, can you? And it would seem to add a certain symmetry to
the language. For example:
const Foo & Foo::Foo(const Foo &r)
{
#if existing_semantics
if (this == &r) // A-1
return *this;
this->memb = r.memb; // A-2
update(this); // A-3
return *this; // A-4
#elif new_semantics
if (&this == &r) // B-1
return this;
this.memb = r.memb; // B-2
update(&this); // B-3
return this; // B-4
#endif
}
Please don't tell me that we can't possibly change the language
now, because I know that already. But we could allow the '.' and
'->' operators to be used interchangeably with 'this' operand
without breaking any existing code.
-- David R. Tribble, dtribble@technologist.com --
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]
Author: Francis Glassborow <francis@robinton.demon.co.uk>
Date: 1999/05/07 Raw View
In article <37322947.E776B089@technologist.com>, David R Tribble
<dtribble@technologist.com> writes
>But still, wouldn't it make as much sense for 'this' to be a
>reference as to be a pointer? After all, you can't modify the 'this'
>pointer, can you? And it would seem to add a certain symmetry to
>the language. For example:
But once upon a time you could do exactly that (modify this)
Francis Glassborow Journal Editor, Association of C & C++ Users
64 Southfield Rd
Oxford OX4 1PA +44(0)1865 246490
All opinions are mine and do not represent those of any organisation
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html ]