Why is there destructor in union?

Discussion in 'C++' started by piboye, Nov 16, 2006.

  1. piboye

    piboye Guest

    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.
     
    piboye, Nov 16, 2006
    #1
    1. Advertising

  2. piboye

    Salt_Peter Guest

    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.
     
    Salt_Peter, Nov 16, 2006
    #2
    1. Advertising

  3. piboye

    piboye Guest

    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 дµÀ£º
    "
    > 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, Nov 16, 2006
    #3
  4. piboye

    Kai-Uwe Bux Guest

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


    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
     
    Kai-Uwe Bux, Nov 16, 2006
    #4
  5. piboye

    Kai-Uwe Bux Guest

    Please do not top-post in this group. It is frowned upon by regulars.

    piboye wrote:

    > 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
     
    Kai-Uwe Bux, Nov 16, 2006
    #5
  6. piboye

    piboye Guest

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

    >
    > 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
     
    piboye, Nov 16, 2006
    #6
  7. piboye

    piboye Guest

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

    >
    > 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.
     
    piboye, Nov 16, 2006
    #7
  8. piboye

    piboye Guest

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

    >
    > 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
     
    piboye, Nov 16, 2006
    #8
  9. >>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?

    Stuart
     
    Stuart Redmann, Nov 16, 2006
    #9
  10. piboye

    Kai-Uwe Bux Guest

    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
     
    Kai-Uwe Bux, Nov 16, 2006
    #10
  11. piboye

    piboye Guest

    "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
     
    piboye, Nov 16, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt Garman
    Replies:
    1
    Views:
    691
    Matt Garman
    Apr 25, 2004
  2. Peter Dunker

    union in struct without union name

    Peter Dunker, Apr 26, 2004, in forum: C Programming
    Replies:
    2
    Views:
    924
    Chris Torek
    Apr 26, 2004
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    2,225
    Smokey Grindel
    Dec 2, 2006
  4. piboye
    Replies:
    0
    Views:
    299
    piboye
    Nov 16, 2006
  5. piboye
    Replies:
    1
    Views:
    314
Loading...

Share This Page