Topic: Should it be enforce is an error?
Author: "Al Grant" <tnarga@arm.REVERSE-NAME.com>
Date: Fri, 29 Mar 2002 04:29:34 CST Raw View
"Carl Daniel" <cpdaniel@pacbell.net> wrote in message
news:cUqo8.59022$Cl1.1689730218@newssvr14.news.prodigy.com...
> "Shao Wu" <swu00@earthlink.net> wrote in message
> news:3CA23662.1967FC67@earthlink.net...
> > Unlike constructors C++ allows direct invokation of destructors.
Objects
> > manage their own life time can invoke the destructor. Example:
> > class X {
> > public:
> > void destroy() { ~X(); } // or this->~X()
>
> That doesn't de-allocate the memory, it simply destroys the object.
No, it creates a temporary X and applies the ~ operator to it.
(But the form in the comment is right.)
---
[ 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://www.research.att.com/~austern/csc/faq.html ]
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Wed, 27 Mar 2002 18:22:20 GMT Raw View
In article <3CA10C10.E4DAE1A4@earthlink.net>, Shao Wu
<swu00@earthlink.net> writes
>I came across one silly but interesting question. Here is it is:
> class X {
> public:
> ~X();
> ... other stuff ...
> };
>
> X::~X()
> {
> delete this;
> }
>
>Everyone knows that this is a bug and I'm sure nobody would do
>such a thing in real-life programming. My question is, should
>"delete this" be enforce as an error at compile time? Some
>compilers not even generate a warning. Any thoughts?
The compiler is not required to detect recursive stupidity, and strictly
speaking there is nothing wrong with it unless you call that dtor.
--
Francis Glassborow
Check out the ACCU Spring Conference 2002
4 Days, 4 tracks, 4+ languages, World class speakers
For details see: http://www.accu.org/events/public/accu0204.htm
---
[ 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://www.research.att.com/~austern/csc/faq.html ]
Author: Shao Wu <swu00@earthlink.net>
Date: Wed, 27 Mar 2002 19:42:09 GMT Raw View
Francis Glassborow wrote:
> In article <3CA10C10.E4DAE1A4@earthlink.net>, Shao Wu
> <swu00@earthlink.net> writes
> >I came across one silly but interesting question. Here is it is:
> > class X {
> > public:
> > ~X();
> > ... other stuff ...
> > };
> >
> > X::~X()
> > {
> > delete this;
> > }
> >
> >Everyone knows that this is a bug and I'm sure nobody would do
> >such a thing in real-life programming. My question is, should
> >"delete this" be enforce as an error at compile time? Some
> >compilers not even generate a warning. Any thoughts?
>
> The compiler is not required to detect recursive stupidity, and strictly
> speaking there is nothing wrong with it unless you call that dtor.
I agree that compilers should not require to detect recursive (and
they often do for optimization purpose). But the real question
is really: should "delete this" be a compile time error.
C++ disallow assignment to "this" pointer because "this" is a
special pointer having an implicit "const" attribute. If "this" allowed
to be assigned, this silly code would be valid (and may have nightmare
results):
X &X::operator=(const X &x)
{
if (this == &x)
return *this;
delete this; /// destroy the current object
this = new X(x); /// re-create one
return *this;
}
If the above code were valid, after the assignment completes its
execution, the left value would have a completely different address!
By disallowing assignment to "this" eliminates problems like this.
By the same token, "delete this" in anywhere of any member
function does not make sense, I think it might be a good idea for
the language to define that as a compilation error -- "delete this"
really has no practical semantic meaning any way, except would
lead to runtime error.
Regards,
Shao Wu.
---
[ 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://www.research.att.com/~austern/csc/faq.html ]
Author: "Carl Daniel" <cpdaniel@pacbell.net>
Date: Wed, 27 Mar 2002 20:20:03 GMT Raw View
"Shao Wu" <swu00@earthlink.net> wrote in message
news:3CA21583.E0AF1DB@earthlink.net...
> Francis Glassborow wrote:
> > The compiler is not required to detect recursive stupidity, and strictly
> > speaking there is nothing wrong with it unless you call that dtor.
>
> I agree that compilers should not require to detect recursive (and
> they often do for optimization purpose). But the real question
> is really: should "delete this" be a compile time error.
No, 'delete this;' must be a legal construct. It's not uncommonly used,
particularly in Windows COM programming, but in general in any
implementation of an object which manages it's own lifetime. Such classes
generally also take pains to ensure that they're allocated on the heap (e.g.
by using one of the several Factory patterns and making the constructor(s)
private/protected).
-cd
---
[ 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://www.research.att.com/~austern/csc/faq.html ]
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Wed, 27 Mar 2002 21:00:00 GMT Raw View
In article <3CA21583.E0AF1DB@earthlink.net>, Shao Wu
<swu00@earthlink.net> writes
>If the above code were valid, after the assignment completes its
>execution, the left value would have a completely different address!
>By disallowing assignment to "this" eliminates problems like this.
>By the same token, "delete this" in anywhere of any member
>function does not make sense, I think it might be a good idea for
>the language to define that as a compilation error -- "delete this"
>really has no practical semantic meaning any way, except would
>lead to runtime error.
Oh, I see you were wanting rehash that argument. And while I think that
delete this needs to be used carefully, I would not want to disallow it
because it is used constructively for some programming.
--
Francis Glassborow
Check out the ACCU Spring Conference 2002
4 Days, 4 tracks, 4+ languages, World class speakers
For details see: http://www.accu.org/events/public/accu0204.htm
---
[ 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://www.research.att.com/~austern/csc/faq.html ]
Author: Shao Wu <swu00@earthlink.net>
Date: Wed, 27 Mar 2002 21:26:15 GMT Raw View
Carl Daniel wrote:
> "Shao Wu" <swu00@earthlink.net> wrote in message
> news:3CA21583.E0AF1DB@earthlink.net...
> > Francis Glassborow wrote:
> > > The compiler is not required to detect recursive stupidity, and strictly
> > > speaking there is nothing wrong with it unless you call that dtor.
> >
> > I agree that compilers should not require to detect recursive (and
> > they often do for optimization purpose). But the real question
> > is really: should "delete this" be a compile time error.
>
> No, 'delete this;' must be a legal construct. It's not uncommonly used,
> particularly in Windows COM programming, but in general in any
> implementation of an object which manages it's own lifetime. Such classes
> generally also take pains to ensure that they're allocated on the heap (e.g.
> by using one of the several Factory patterns and making the constructor(s)
> private/protected).
>
> -cd
Unlike constructors C++ allows direct invokation of destructors. Objects
manage their own life time can invoke the destructor. Example:
class X {
public:
void destroy() { ~X(); } // or this->~X()
...
private:
~X();
...
};
Calling the destructor directly shows the clear intent of the programmer, yet
the equivalent :
void X::destroy() { delete this; }
would lead to object recreation ("this = new X;"). Disallowing "delete this"
does not loose anything in the language. Pairing "delete this"
and "this = ..." (assign to "this") and disallowing both would make
"this" more orthagonal (spell?) because "this" pointer is a special pointer
and not allow to be temper with.
I do see that "delete this" would make automated code generator a little bit
easier to write. Emitting "delete this" is easier than "object_destructor()".
But that should be easy enough to fix since generators has to keep track of
class names any way.
Regards,
Shao Wu
---
[ 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://www.research.att.com/~austern/csc/faq.html ]
Author: "Carl Daniel" <cpdaniel@pacbell.net>
Date: Wed, 27 Mar 2002 21:36:15 GMT Raw View
"Shao Wu" <swu00@earthlink.net> wrote in message
news:3CA23662.1967FC67@earthlink.net...
> Carl Daniel wrote:
> > No, 'delete this;' must be a legal construct. It's not uncommonly used,
>
> Unlike constructors C++ allows direct invokation of destructors. Objects
> manage their own life time can invoke the destructor. Example:
> class X {
> public:
> void destroy() { ~X(); } // or this->~X()
That doesn't de-allocate the memory, it simply destroys the object. NOT
equivalent.
-cd
---
[ 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://www.research.att.com/~austern/csc/faq.html ]
Author: Shao Wu <swu00@earthlink.net>
Date: Wed, 27 Mar 2002 13:12:52 GMT Raw View
Hi All,
I came across one silly but interesting question. Here is it is:
class X {
public:
~X();
... other stuff ...
};
X::~X()
{
delete this;
}
Everyone knows that this is a bug and I'm sure nobody would do
such a thing in real-life programming. My question is, should
"delete this" be enforce as an error at compile time? Some
compilers not even generate a warning. Any thoughts?
Regards,
Shao Wu.
---
[ 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://www.research.att.com/~austern/csc/faq.html ]