object destruction

Discussion in 'C++' started by EN, Mar 15, 2005.

  1. EN

    EN Guest

    Hi.

    I've small problem :
    I've two objects, each from another class:
    example object a from class A and b from B.
    I method from class A :
    a.setValue() I made object b:
    B *b = new B();

    What is happen when in main program I invoke delete to object a,
    object b is destroy to???

    example:

    int main(){
    A *a = new A();
    a->setValue(4);
    delete a;
    }

    Thanks in advice
    EN
     
    EN, Mar 15, 2005
    #1
    1. Advertising

  2. EN

    Sharad Kala Guest

    "EN" <> wrote in message
    > Hi.
    >
    > I've small problem :
    > I've two objects, each from another class:
    > example object a from class A and b from B.
    > I method from class A :
    > a.setValue() I made object b:
    > B *b = new B();
    >
    > What is happen when in main program I invoke delete to object a,
    > object b is destroy to???
    >
    > example:
    >
    > int main(){
    > A *a = new A();
    > a->setValue(4);
    > delete a;
    > }
    >

    Your language is not clear but b won't get destroyed when a is deleted. You
    have to delete it explicitly.

    Sharad
     
    Sharad Kala, Mar 15, 2005
    #2
    1. Advertising

  3. EN

    Rolf Magnus Guest

    EN wrote:

    > Hi.
    >
    > I've small problem :
    > I've two objects, each from another class:
    > example object a from class A and b from B.


    Your example code below only contains the former. Where is object b?

    > I method from class A :
    > a.setValue() I made object b:
    > B *b = new B();
    >
    > What is happen when in main program I invoke delete to object a,
    > object b is destroy to???


    Well, when you got it from new, it has to be deleted explicitly.

    > example:
    >
    > int main(){
    > A *a = new A();
    > a->setValue(4);
    > delete a;
    > }
     
    Rolf Magnus, Mar 15, 2005
    #3
  4. EN

    Leon Mergen Guest

    On Tue, 2005-03-15 at 11:57 +0100, EN wrote:
    > What is happen when in main program I invoke delete to object a,
    > object b is destroy to???


    No, unless you specify it in a constructor. A common design practice I
    use is delete an object in the same class as it is created. It avoids
    confusion and keeps pointer management centralized.

    Leon Mergen
    http://www.solatis.com/
     
    Leon Mergen, Mar 15, 2005
    #4
    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. johny smith

    question about object destruction

    johny smith, May 9, 2004, in forum: C++
    Replies:
    4
    Views:
    339
    Andrey Tarasevich
    May 10, 2004
  2. Pablo
    Replies:
    6
    Views:
    345
    Howard
    Aug 18, 2006
  3. V Patel
    Replies:
    5
    Views:
    321
    Yahooooooooo
    Jan 30, 2007
  4. kalki70

    scope and object destruction

    kalki70, Feb 7, 2007, in forum: C++
    Replies:
    2
    Views:
    322
    kalki70
    Feb 7, 2007
  5. Fei Liu
    Replies:
    3
    Views:
    562
    Roland Pibinger
    Apr 25, 2007
Loading...

Share This Page