473,320 Members | 1,828 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,320 software developers and data experts.

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 4295

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

52
by: Newsnet Customer | last post by:
Hi, Statement 1: "A dynamically created local object will call it's destructor method when it goes out of scope when a procedure returms" Agree. Statement 2: "A dynamically created object...
11
by: Stub | last post by:
Please answer my questions below - thanks! 1. Why "Derived constructor" is called but "Derived destructor" not in Case 1 since object B is new'ed from Derived class? 2. Why "Derived destructor"...
35
by: Peter Oliphant | last post by:
I'm programming in VS C++.NET 2005 using cli:/pure syntax. In my code I have a class derived from Form that creates an instance of one of my custom classes via gcnew and stores the pointer in a...
2
by: arun | last post by:
Hello Group, I have a class A, which has three other members and each member is of a different class type. If I don't create a destructor then the compiler will give me one and whenever...
17
by: Phlip | last post by:
C++ers: I have this friend who thinks C++ cannot throw from a destructor. I think throwing from a destructor is bad Karma, and should be designed around, but that C++ cannot strictly prevent...
7
by: engaarea | last post by:
hi all, What are the circumstances in which one would have to call a destructor explicitly. Regards 5
4
by: Morgan Cheng | last post by:
In C#, we can initialize static member variables in static constructor. Now, when to destruct these static member variables? If these variables hold external reference(e.g. log file stream), it is...
10
by: Szabolcs Horvát | last post by:
Consider the attached example program: an object of type 'A' is inserted into a 'map<int, Am;'. Why does 'm;' call the copy constructor of 'A' twice in addition to a constructor call? The...
10
by: piboye | last post by:
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...
7
by: krishna81m | last post by:
In the following code snippet, I created a class samp which has two private variables /char/ and /char*/ which are initialized in the constructor. I create a function /samp input()/ that returns an...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.