Is this object destroyed?

Discussion in 'C++' started by Anon Email, Dec 7, 2003.

  1. Anon Email

    Anon Email Guest

    In the code below, when the member function AddInput is called, a new
    object aNum is created, of type InputNum. Actually, AddInput is called
    twice, thus creating two separate aNum objects. I gather that after
    AddInput is executed, each separate aNum object is destroyed? Is no
    destructor required?

    Cheers,

    Deets


    #include <iostream>
    using std::cout;
    using std::cin;
    using std::endl;

    class InputNum
    {
    public:
    InputNum (char msg [])
    {
    cout << msg;
    cin >> _num;
    }

    int GetValue () const { return _num; }

    void AddInput (char msg [])
    {
    InputNum aNum (msg);
    _num = GetValue () + aNum.GetValue ();
    cout << aNum.GetValue ();
    }


    private:
    int _num;
    };

    char SumString[] = "The sum is ";

    int main()
    {
    InputNum num ("Enter number ");
    num.AddInput ("Another one ");
    num.AddInput ("One more ");
    cout << SumString << num.GetValue () << endl;
    }
     
    Anon Email, Dec 7, 2003
    #1
    1. Advertising

  2. Anon Email

    Moonlit Guest

    "Anon Email" <> wrote in message
    news:...
    > In the code below, when the member function AddInput is called, a new
    > object aNum is created, of type InputNum. Actually, AddInput is called
    > twice, thus creating two separate aNum objects. I gather that after
    > AddInput is executed, each separate aNum object is destroyed? Is no
    > destructor required?

    Correct. It is created on the stack. On returning from the subroutine the
    destructor for aNum is called. Since nothing special is done the default
    constructor suffice

    So nothing has to be done.

    Regards, Ron AF Greve.

    >
    > Cheers,
    >
    > Deets
    >
    >
    > #include <iostream>
    > using std::cout;
    > using std::cin;
    > using std::endl;
    >
    > class InputNum
    > {
    > public:
    > InputNum (char msg [])
    > {
    > cout << msg;
    > cin >> _num;
    > }
    >
    > int GetValue () const { return _num; }
    >
    > void AddInput (char msg [])
    > {
    > InputNum aNum (msg);
    > _num = GetValue () + aNum.GetValue ();
    > cout << aNum.GetValue ();
    > }
    >
    >
    > private:
    > int _num;
    > };
    >
    > char SumString[] = "The sum is ";
    >
    > int main()
    > {
    > InputNum num ("Enter number ");
    > num.AddInput ("Another one ");
    > num.AddInput ("One more ");
    > cout << SumString << num.GetValue () << endl;
    > }
     
    Moonlit, Dec 7, 2003
    #2
    1. Advertising

  3. Anon Email

    Anon Email Guest

    Thanks, Ron.
     
    Anon Email, Dec 8, 2003
    #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.
Similar Threads
  1. enrique
    Replies:
    8
    Views:
    554
    Kevin McMurtrie
    Apr 29, 2005
  2. Charles Herman

    when is object destroyed?

    Charles Herman, Nov 6, 2003, in forum: C++
    Replies:
    2
    Views:
    432
    lilburne
    Nov 6, 2003
  3. Ioannis Vranos
    Replies:
    9
    Views:
    316
    Ioannis Vranos
    May 16, 2005
  4. Nick Jacobson

    Enumerate object is destroyed by casting?

    Nick Jacobson, Aug 25, 2004, in forum: Python
    Replies:
    2
    Views:
    290
    Shalabh Chaturvedi
    Aug 25, 2004
  5. Raga
    Replies:
    0
    Views:
    297
Loading...

Share This Page