By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,597 Members | 1,307 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,597 IT Pros & Developers. It's quick & easy.

Why is there destructor in union?

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a

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.