Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why is there destructor in union?

Reply
Thread Tools

Why is there destructor in union?

 
 
piboye
Guest
Posts: n/a
 
      11-16-2006

"Kai-Uwe Bux д
"
> Stuart Redmann wrote:
>
> >>>piboye wrote:
> >>>
> >>>>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.
> >>>
> >>>Don't kid yourself. If a compiler cannot generate a destructor, it
> >>>can't compile the program. Destructors are absolutely required
> >>>(exception: PODs). Just because you don't see them being invoked does
> >>>not mean they aren't synthesized.
> >>>
> >>>A destructor's job is to destroy the object (to invoke member d~tors
> >>>and recover any reserved memory). So its job is absolutely critical.

> >
> > piboye wrote:
> >> to Salt Peter:
> >> The problem is that the member of union must be type of POD .
> >> POD is just no destructor. no destructor just no hold
> >> resources,yet needless to recover memory or other resources.
> >> your answer is cover about Object ang Class,not special for
> >> union.
> >>
> >> I have the question why do union's destructor exist?

> >
> > What if the union contains some pointers to memory that is owned by the
> > union, like this is the case for the VARIANT data type under windows?
> > Wouldn't it be nice if the union knew itself how to release these
> > resources?

>
> Hm, how:
>
> union X {
>
> int * ip;
> float * fp;
>
> X ( int i )
> : ip ( new int (i) )
> {}
>
> X ( float f )
> : fp ( new float (f) )
> {}
>
> ~X ( void ) {
> // this looks fishy:
> delete ip;
> delete fp;
> }
>
> };
>
> I would expect the destructor to yield undefined behavior.
>
>
> Best
>
> Kai-Uwe Bux

Hi
I think so to you. In union's destructor ,pointer was deleted
twice.

> ~X ( void ) {
> // this looks fishy:
> delete ip;
> delete fp;
> }


Therefore, the usage of union's destructors is no plenitude reason
for why union's destructors be allowed to use.

But, I thanks sincerely for your help.

Best

Piboye Liu

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
newbie -- smart pointer destructor called without destructor everbeing called Jimmy Hartzell C++ 2 05-20-2008 02:20 AM
newbie -- smart pointer destructor called without destructor everbeing called Jimmy Hartzell C++ 0 05-19-2008 07:05 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
compiler generated destructor vs class implemented destructor arun C++ 2 06-13-2006 05:43 AM
Explicit destructor calls from inside base class destructor frs C++ 20 09-21-2005 09:22 AM



Advertisments