Topic: Calling a CONSTRUCTOR from a DESTRUCTOR
Author: eswar@kangri.EBay.Sun.COM (Badari Eswar)
Date: 9 Aug 91 23:01:19 GMT Raw View
Perhaps this has been discussed before on the net. The following program
ends up in an infinite loop. The question then is whether an instantiation
of an object be allowed in a destructor of the object?
eswar......
-----------------------------------------------------------------------------
#include <stream.h>
class B {
public:
B() { cout << "In B()\n"; }
~B() { B b ; //Should this be allowed?
cout << "In ~B()\n";
}
} ;
main() {
B b;
}
-----------------------------------------------------------------------------
--
eswar..........
-------------------------------------------------------------------------------
B.S. Eswar
Sales Systems & Development
Author: glenn@bitstream.com (Glenn P. Parker)
Date: 12 Aug 91 13:46:06 GMT Raw View
In article <7888@male.EBay.Sun.COM> eswar@kangri.EBay.Sun.COM (Badari Eswar) writes:
> Perhaps this has been discussed before on the net. The following program
> ends up in an infinite loop. The question then is whether an instantiation
> of an object be allowed in a destructor of the object?
> class B {
> public:
> B() { cout << "In B()\n"; }
> ~B() { B b ; //Should this be allowed?
> cout << "In ~B()\n";
> }
> } ;
>
> main() {
> B b;
> }
Of course it should be allowed, and it *is* allowed. This is a simply a
recursive function with no termination condition. The compiler didn't stop
you from hanging yourself with your own rope, that's all :-). If you
wanted to make this a (slightly more) useful construct, you might consider
changing the destructor to something like the following:
class B {
static int flag;
public:
B() { cout << "In B()\n"; }
~B()
{
if(flag)
{
B b;
flag = 0;
}
cout << "In ~B()\n";
}
};
--
Glenn P. Parker glenn@bitstream.com Bitstream, Inc.
uunet!huxley!glenn 215 First Street
BIX: parker Cambridge, MA 02142-1270
Author: steve@taumet.com (Stephen D. Clamage)
Date: 13 Aug 91 16:08:15 GMT Raw View
eswar@kangri.EBay.Sun.COM (Badari Eswar) writes:
>The following program
>ends up in an infinite loop. The question then is whether an instantiation
>of an object be allowed in a destructor of the object?
>class B {
> public:
> ~B() { B b ; ... }
>} ;
Sure it is allowed. It is also allowed to write
while(1) ;
or
A: goto A;
which are more visible infinite loops.
It is not in general possible to determine whether a program will
terminate (this is a fundamental theorem in computer science), so
non-terminating programs cannot be made illegal.
I don't see any good reason to make up a list of kinds of things
like the above examples which are "known" not to terminate and
make them illegal. There may be good reasons to write any of these
examples, assuming some external signal is being awaited.
--
Steve Clamage, TauMetric Corp, steve@taumet.com