473,406 Members | 2,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Deconstructors

Kip
Greetings,

It would seem as though whenever I find a feature that is not in C /
C++, I discover later that it was not implemented for good reason.

I am curious. What was the rationale behind not allowing deconstructors
to take parameters? Or can they and I am mistaken?

--
Kip Warner
Vertigo
http://TheVertigo.com
Jul 23 '05 #1
11 2529
"Kip" <Kip@_NOSPAM_TheVertigo.com> wrote in message
news:xn***************@news.telus.net...
I am curious. What was the rationale behind not allowing deconstructors
to take parameters? Or can they and I am mistaken?


If they did, how would you use them?
Jul 23 '05 #2
Kip wrote:
It would seem as though whenever I find a feature that is not in C /
C++, I discover later that it was not implemented for good reason.

I am curious. What was the rationale behind not allowing
deconstructors to take parameters? Or can they and I am mistaken?


Destructors are most often called implicitly. If you define one that
takes arguments, what arguments would you want the system to pass?

V
Jul 23 '05 #3
* Andrew Koenig:
"Kip" <Kip@_NOSPAM_TheVertigo.com> wrote in message
news:xn***************@news.telus.net...
I am curious. What was the rationale behind not allowing deconstructors
to take parameters? Or can they and I am mistaken?


If they did, how would you use them?


One idea that has been floating around is a bool argument that is 'true' if
and only if an exception thrown by the destructor would be caught by some
catch-block or by a hypothetical "..." function try block around 'main' (or
some other specific rule, it may be that this particular one is brain-dead).

Not that I think it's a good idea in general to throw from destructors.

But as I recall one proposed usage of that idea was some kind of scope guard
that threw (?) an exception from its destructor if it hadn't received a
commit() call.

--
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?
Jul 23 '05 #4
Alf P. Steinbach wrote:

One idea that has been floating around is a bool argument that is 'true' if
and only if an exception thrown by the destructor would be caught by some
catch-block or by a hypothetical "..." function try block around 'main' (or
some other specific rule, it may be that this particular one is brain-dead).


That also requires two-phase exception handling, which is not currently
required. (i.e. the implementation is currently allowed to destroy as it
unwinds, without knowing in advance whether the exception will be
caught) So it's not just adding an argument; it's fundamentally
redesigning exceptions.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #5
Victor Bazarov wrote:
Destructors are most often called implicitly. If you define one
that takes arguments, what arguments would you want the system to
pass?


None, just like with constructors.

I think your argument applies just as well to constructors, which,
after all, are called implicitly with no parameters when an array
is declared. But we don't consider it a problem that one can also
write constructors that take parameters.

However, I suggest that putting destructors with parameters into
the language is still a bad idea. Here is why:

Reason 1

Conceptually, there are usually lots of ways to make something,
but only one way to make it go away.

On the off chance that you can think of several ways to destroy an
object, you can still implement this in C++: store some indication
in the object about how it should be destroyed. The destructor
looks at this and acts accordingly.

Thus, multiple destruction methods can already be done, and it
seems rare enough that it is hardly worth adding new complexity
to the language.

Reason 2

{
Foo x;
x.~Foo(param);
}

x gets destroyed twice: once when I do it explicitly, and once
implicitly when it goes out of scope. This is bad.

Of course, one can call a destructor explicitly even in the current
version of C++, but it is rare, and so when we do it, we often
think carefully about what is happening.

So the real problem with destructors with parameters is that they
make calling a destructor explicitly seem "normal", despite the
fact that it is dangerous.

Jul 23 '05 #6
On Sat, 26 Mar 2005 23:16:04 GMT, "Kip" <Kip@_NOSPAM_TheVertigo.com> wrote:
Greetings,

It would seem as though whenever I find a feature that is not in C /
C++, I discover later that it was not implemented for good reason.

I am curious. What was the rationale behind not allowing deconstructors
to take parameters? Or can they and I am mistaken?


You could fake destructors taking parameters by setting some members just
before the object gets destroyed. For example:

class A {
public:
int dest_param;

A::~A()
{
switch (dest_param)
{
default:
/* ... */
break;
}
}
}

int main ()
{
A inst1;

inst1.dest_param = 123;
/* Destructor gets called at function exit. */
return 0;
}

There isn't a more direct way of doing this - as another poster mentioned,
destructors are called implicitly when an object falls out of scope.

FYI, you can directly pass paramers to the delete operators, but these
might not be as useful for the object that you are destroying as the
example shown above.
Jul 23 '05 #7
* Pete Becker:
Alf P. Steinbach wrote:

One idea that has been floating around is a bool argument that is 'true' if
and only if an exception thrown by the destructor would be caught by some
catch-block or by a hypothetical "..." function try block around 'main' (or
some other specific rule, it may be that this particular one is brain-dead).


That also requires two-phase exception handling


Yes, and it doesn't even tell whether the destructor is called as a result of
stack unwinding or not.

A simpler and more efficient rule could be to have the argument specify
whether the destructor is called directly by exception-initiated stack
unwinding.

That would support the scope guard application (which on reflection the
brain-dead rule doesn't) but not much else.

--
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?
Jul 23 '05 #8
Kip
Andrew Koenig wrote:
"Kip" <Kip@_NOSPAM_TheVertigo.com> wrote in message
news:xn***************@news.telus.net...
I am curious. What was the rationale behind not allowing
deconstructors to take parameters? Or can they and I am mistaken?


If they did, how would you use them?


CThing *pThing = new CThing;

delete pThing(5);

--
Kip Warner
Vertigo
http://TheVertigo.com
Jul 23 '05 #9
Kip schrieb:
Andrew Koenig wrote:

"Kip" <Kip@_NOSPAM_TheVertigo.com> wrote in message
news:xn***************@news.telus.net...

I am curious. What was the rationale behind not allowing
deconstructors to take parameters? Or can they and I am mistaken?


If they did, how would you use them?

CThing *pThing = new CThing;

delete pThing(5);


But what if the thing is on the stack?

void func()
{
CThing thing;
thing.doStuff();
// and now?
thing.~thing(5);
// bad, default dtor still implicitly called at end of scope
}

Cheers,
Malte
Jul 23 '05 #10

"Kip" <Kip@_NOSPAM_TheVertigo.com> wrote in message
news:xn***************@news.telus.net...
Andrew Koenig wrote:
"Kip" <Kip@_NOSPAM_TheVertigo.com> wrote in message
news:xn***************@news.telus.net...
> I am curious. What was the rationale behind not allowing
> deconstructors to take parameters? Or can they and I am mistaken?


If they did, how would you use them?


CThing *pThing = new CThing;

delete pThing(5);


I think what was meant was, how whould you use that information? I'm not
sure what, in a destructor, would be useful to know, since the object is
going away anyway and any state changes you make to it will be lost. And
anything that you need to do prior to destruction can be done via a member
function (or by directly accessing public members).

Or, if you need to take some action before destruction, regardless of how
that destruction is going to occur, then you could consider containing your
object inside another object, whose job is to call a member function of that
inner object, prior to allowing everything to be destroyed.

I suspect that the designers simply felt there wasn't any need for such a
construct. Perhaps you have such a need? I sure can't think of one (except
for the idea discussed here regarding exceptions, but that's opening a
bigger can of worms).

-Howard
Jul 23 '05 #11
"Howard" <al*****@hotmail.com> wrote in message
news:Sp********************@bgtnsc05-news.ops.worldnet.att.net...
"Kip" <Kip@_NOSPAM_TheVertigo.com> wrote in message
news:xn***************@news.telus.net...
Andrew Koenig wrote:
"Kip" <Kip@_NOSPAM_TheVertigo.com> wrote in message
news:xn***************@news.telus.net...

> I am curious. What was the rationale behind not allowing
> deconstructors to take parameters? Or can they and I am mistaken?

If they did, how would you use them?


CThing *pThing = new CThing;

delete pThing(5);


I think what was meant was, how whould you use that information? I'm not
sure what, in a destructor, would be useful to know, since the object is
going away anyway and any state changes you make to it will be lost. And
anything that you need to do prior to destruction can be done via a member
function (or by directly accessing public members).


Moreover, this example doesn't really answer the question, because it covers
only one, fairly uncommon, case. For example:

{
CThing x;
}

At the }, how do you say what argument to give x's destructor?

Or what about this case?

CThing *p = new CThing[100];
delete [] p;

What if you want to give a different destructor argument to each element of
the array?

Or this case?

std::vector<CThing> v;

How do you tell v what arguments to give to destructors when it destroys v's
elements?

This last case is the most interesting, because it is an example of a more
general problem: The part of a program that constructs and destroys objects
of a given type may well be written separately from the definition of that
type itself. So how does one part of the program tell the other part what
destructor arguments to use?
Jul 23 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Mei | last post by:
I have some classes at the moment that are initialised with their own function rather than a constructor and destructor. I wish this case to be reversed if possible. For arguments sake (you dont...
3
by: N4M | last post by:
Dear, I have problems with overloaded operators ++() and --(). MSVC++ 6.0 compiler gives errors, one is shown as below: " c:\data\c++\mygraphs\graph.h(182) : error C2555: 'CGraphNodeIter::++' :...
6
by: MR | last post by:
i would appreciate some assistance in finding a reference that describes how to set up a tcp/ip server/listener in c#. also samples would come in handy. thanks m
13
by: Mark Rae | last post by:
Hi, Since sealed classes can't be instantiated with the new keyword e.g. CClass objClass = new CClass(), does this mean that they don't have constructors / deconstructors or, if they do, that...
6
by: Alden Pierre | last post by:
Hello, http://www.parashift.com/c++-faq-lite/virtual-functions.html#faq-20.7 As per the link above it's wise to have a virtual deconstructor when creating an abstract class. Here is when I'm...
8
by: acheron05 | last post by:
Hi there, Hopefully I won't fit the stereotype of the typical student posting his assignment to be written for him, but I am quite stuck. If anyone could give me some help in how to create...
4
by: Mohsen | last post by:
Hello everybody, I have written a program and I have included 5 other programs (which I have borrowed 3 of them) in my program. Every time that I change the size of my arrays (pointers) I get...
1
by: raistlinx | last post by:
When you create a template class, what do you do about deconstructors? Given the class below I added ~DLNode(){}; after the constructor, but when I ran some tests it didn't seem to ever fire when...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.