difference between compiler generated destructor and class implemented

Discussion in 'C++' started by arun, Jun 12, 2006.

  1. arun

    arun Guest

    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 class A is
    destroyed (whether on stack or on heap) the destructor of each
    different object will be called.

    However what will happen if the class A defines a destructor like

    ~A() {} // No code.

    The question is that in this case will the destructor of individual
    members be called or not,
    since the destructor code is emply?

    The whole code will look like:

    class Object1 {

    // Code destructor, constructor

    }
    class Object2 {

    // Code destructor, constructor

    }

    class Object3 {

    // Code destructor, constructor

    }

    class A {


    Object1 o1;
    Object2 o2;
    Object3 o3;

    ~A() {} // The question is all about

    }


    Thanks

    nagrik
     
    arun, Jun 12, 2006
    #1
    1. Advertising

  2. arun

    Noah Roberts Guest

    arun wrote:
    > 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 class A is
    > destroyed (whether on stack or on heap) the destructor of each
    > different object will be called.
    >
    > However what will happen if the class A defines a destructor like
    >
    > ~A() {} // No code.


    Same thing really.

    Often base classes define this destructor:

    virtual ~Base() {}

    Same thing as default constructor but is virtual and so this class can
    be safely derived.

    >
    > The question is that in this case will the destructor of individual
    > members be called or not,
    > since the destructor code is emply?


    Yes. Member variables are destroyed when the lifetime of the
    containing object terminates - or is "destroyed".
     
    Noah Roberts, Jun 12, 2006
    #2
    1. Advertising

  3. arun wrote:
    > 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 class A is
    > destroyed (whether on stack or on heap) the destructor of each
    > different object will be called.
    >
    > However what will happen if the class A defines a destructor like
    >
    > ~A() {} // No code.
    >
    > The question is that in this case will the destructor of individual
    > members be called or not,
    > since the destructor code is emply?
    >
    > The whole code will look like:
    >
    > class Object1 {
    >
    > // Code destructor, constructor
    >
    > }
    > class Object2 {
    >
    > // Code destructor, constructor
    >
    > }
    >
    > class Object3 {
    >
    > // Code destructor, constructor
    >
    > }
    >
    > class A {
    >
    >
    > Object1 o1;
    > Object2 o2;
    > Object3 o3;
    >
    > ~A() {} // The question is all about
    >
    > }
    >
    >
    > Thanks
    >
    > nagrik
    >

    Just remember that when any object goes out of scope its destructor will
    be called. Hence when a object goes out of scope, destructor calls for
    all the sub objects that make up the object itself will occur. So unless
    the destructor needs to be virtual, allocated memory needs to be taken
    care of, or the destruction needs to be flagged elsewhere, it's not
    absolutely necessary to code for it. Some people prefer to always code
    it for completeness

    JB
     
    n2xssvv g02gfr12930, Jun 12, 2006
    #3
    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.

Share This Page