Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is this object destroyed?

Reply
Thread Tools

Is this object destroyed?

 
 
Anon Email
Guest
Posts: n/a
 
      12-07-2003
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;
}
 
Reply With Quote
 
 
 
 
Moonlit
Guest
Posts: n/a
 
      12-07-2003

"Anon Email" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> 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;
> }



 
Reply With Quote
 
 
 
 
Anon Email
Guest
Posts: n/a
 
      12-08-2003
Thanks, Ron.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Error:Object reference not set to an instance of an object. Suresh Kojhani ASP .Net 1 07-29-2004 12:10 PM
Error !Object reference not set to an instance of an object. !!! Help Parthiv Joshi ASP .Net 2 07-02-2004 10:28 AM
Object reference not set to an instance of an object. yysiow ASP .Net 1 07-12-2003 03:30 PM
Object reference not set to an instance of an object. Chris Fink ASP .Net 2 07-03-2003 06:48 PM



Advertisments