Why is there destructor in union?

P

piboye

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.
 
S

Salt_Peter

piboye said:
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.
 
P

piboye

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?
"Salt_Peter дµÀ£º
"
 
K

Kai-Uwe Bux

Salt_Peter said:
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.

I think, you are misinterpreting the OP. The problem is not whether a
destructor will be synthesized. The point is that you are allowed to
specify a destructor for a union:

A union can have member functions (including constructors and
destructors), but not virtual (10.3) functions. [9.5/1]

However, what would you put into such a destructor? You have no idea which
member actually is used, so which member should the destructor work on? The
standard recognizes this problem:

An object of a class with a non-trivial constructor (12.1), a non-trivial
copy constructor (12.8), a non-trivial destructor (12.4), or a non-trivial
copy assignment operator (13.5.3, 12.8) cannot be a member of a union, nor
can an array of such objects.

So, members of a union are required to have trivial destructors. I think the
question of the OP has to be construed as: What is the point of allowing
the union to have a non-trivial destructor when the members have trivial
destructors and cleanup is guaranteed to be automatic anyway?


Best

Kai-Uwe Bux
 
K

Kai-Uwe Bux

Please do not top-post in this group. It is frowned upon by regulars.
to Salt Peter:
The problem is that the member of union must be type of POD .

That is incorrect.
POD is just no destructor. no destructor just no hold
resources,yet needless to recover memory or other resources.

It is true, however, that members in union are required to have trivial
destructors.
your answer is cover about Object ang Class,not special for
union.

I have the question why do union's destructor exist?

Why not? They could write logging information to some file or have some
other external side-effects.


Best

Kai-Uwe Bux
 
P

piboye

Thanks for Kai-Uwe Bux 's tip.

I'm sorry for my badly representation.

Your said
"What is the point of allowing
the union to have a non-trivial destructor when the members have
trivial
destructors and cleanup is guaranteed to be automatic anyway?
".
This is my mean question.



"Kai-Uwe Bux Write:
"
Salt_Peter said:
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.

I think, you are misinterpreting the OP. The problem is not whether a
destructor will be synthesized. The point is that you are allowed to
specify a destructor for a union:

A union can have member functions (including constructors and
destructors), but not virtual (10.3) functions. [9.5/1]

However, what would you put into such a destructor? You have no idea which
member actually is used, so which member should the destructor work on? The
standard recognizes this problem:

An object of a class with a non-trivial constructor (12.1), a non-trivial
copy constructor (12.8), a non-trivial destructor (12.4), or a non-trivial
copy assignment operator (13.5.3, 12.8) cannot be a member of a union, nor
can an array of such objects.

So, members of a union are required to have trivial destructors. I think the
question of the OP has to be construed as: What is the point of allowing
the union to have a non-trivial destructor when the members have trivial
destructors and cleanup is guaranteed to be automatic anyway?


Best

Kai-Uwe Bux
 
P

piboye

"Kai-Uwe Bux дµÀ£º
"
Salt_Peter said:
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.

I think, you are misinterpreting the OP. The problem is not whether a
destructor will be synthesized. The point is that you are allowed to
specify a destructor for a union:

A union can have member functions (including constructors and
destructors), but not virtual (10.3) functions. [9.5/1]

However, what would you put into such a destructor? You have no idea which
member actually is used, so which member should the destructor work on? The
standard recognizes this problem:

An object of a class with a non-trivial constructor (12.1), a non-trivial
copy constructor (12.8), a non-trivial destructor (12.4), or a non-trivial
copy assignment operator (13.5.3, 12.8) cannot be a member of a union, nor
can an array of such objects.

So, members of a union are required to have trivial destructors. I think the
question of the OP has to be construed as: What is the point of allowing
the union to have a non-trivial destructor when the members have trivial
destructors and cleanup is guaranteed to be automatic anyway?


Best

Kai-Uwe Bux

Thanks for Kai-Uwe Bux's tip.

I'm sorry for my badly representation.


Your said
"What is the point of allowing
the union to have a non-trivial destructor when the members have
trivial
destructors and cleanup is guaranteed to be automatic anyway?
".
This is my mean question.
 
P

piboye

"Kai-Uwe Bux дµÀ£º
"
Salt_Peter said:
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.

I think, you are misinterpreting the OP. The problem is not whether a
destructor will be synthesized. The point is that you are allowed to
specify a destructor for a union:

A union can have member functions (including constructors and
destructors), but not virtual (10.3) functions. [9.5/1]

However, what would you put into such a destructor? You have no idea which
member actually is used, so which member should the destructor work on? The
standard recognizes this problem:

An object of a class with a non-trivial constructor (12.1), a non-trivial
copy constructor (12.8), a non-trivial destructor (12.4), or a non-trivial
copy assignment operator (13.5.3, 12.8) cannot be a member of a union, nor
can an array of such objects.

So, members of a union are required to have trivial destructors. I think the
question of the OP has to be construed as: What is the point of allowing
the union to have a non-trivial destructor when the members have trivial
destructors and cleanup is guaranteed to be automatic anyway?


Best

Kai-Uwe Bux

Thanks for Kai-Uwe Bux's tip.

I'm sorry for my badly representation.


Your said
"What is the point of allowing
the union to have a non-trivial destructor when the members have
trivial
destructors and cleanup is guaranteed to be automatic anyway?
".
This is my mean question.

Best
 
S

Stuart Redmann

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?

Stuart
 
K

Kai-Uwe Bux

Stuart said:
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
 
P

piboye

"Kai-Uwe Bux дµÀ£º
"
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top