469,950 Members | 1,431 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

Why is there destructor in union?

Hi !
I'm a academician in china. I have been intereted in C++ lasting.
In reading the C++ Primer book, i have a trouble about union.
In the book ,it said that union can have constructors and destructor

,or other member functions.
I can understand using constructors and memeber functions,but what
is destructor used for?

I have appealled to the forums in chinese ,but no enough usefull
feedback.

Nov 16 '06 #1
1 3868

piboye skrev:
Hi !
I'm a academician in china. I have been intereted in C++ lasting.
In reading the C++ Primer book, i have a trouble about union.
In the book ,it said that union can have constructors and destructor

,or other member functions.
I can understand using constructors and memeber functions,but what
is destructor used for?

I have appealled to the forums in chinese ,but no enough usefull
feedback.
I'm not a big fan of unions so I have not ued them much but I would
expect that their use for constructors and destructors are the same as
that of classes. If, as an example, you in the constructor of the union
use 'new' to allocate some memory for storage of some information then
you'll have to use 'delete' sometime or else you have a memory leak.
Since you'll probably need that information as long as the union is
alive, then the best place to deallocate the memory would be upon the
destruction of the union, thus you place 'delete' in the destructor.

Hope that helps.

--
Erik Wikström

Nov 16 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

52 posts views Thread by Newsnet Customer | last post: by
11 posts views Thread by Stub | last post: by
35 posts views Thread by Peter Oliphant | last post: by
17 posts views Thread by Phlip | last post: by
7 posts views Thread by engaarea | last post: by
4 posts views Thread by Morgan Cheng | last post: by
10 posts views Thread by piboye | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.