Connecting Tech Pros Worldwide Help | Site Map

Instance creation

dave1997_us@yahoo.com
Guest
 
Posts: n/a
#1: Aug 20 '05
Hello,
I get the same o/p for the 2 pieces of code below:
Base *b = new Base ;
b->test() ;
delete b ;

vs just saying:
(new Base)->test() ;

In the second case, how is the object created and what instance of the
object do we have to "delete" at the end of the program. It's a very
fundamental thing which I seem to be getting confused at. Inputs are
welcome.

Thanks

Rolf Magnus
Guest
 
Posts: n/a
#2: Aug 20 '05

re: Instance creation


dave1997_us@yahoo.com wrote:
[color=blue]
> Hello,
> I get the same o/p for the 2 pieces of code below:
> Base *b = new Base ;
> b->test() ;
> delete b ;
>
> vs just saying:
> (new Base)->test() ;[/color]

What is an "o/p"?
[color=blue]
> In the second case, how is the object created[/color]

Just the same as in the first case. Dynamically with new.
[color=blue]
> and what instance of the object do we have to "delete" at the end of the
> program.[/color]

Well, you didn't store the pointer to it, so unless Base's constructor or
the test() member function stores it somewhere, that pointer is lost and
you cannot delete the object.

dave1997_us@yahoo.com
Guest
 
Posts: n/a
#3: Aug 20 '05

re: Instance creation


o/p as in output.
[color=blue]
> Well, you didn't store the pointer to it, so unless Base's
> constructor or the test() member function stores it
> somewhere, that pointer is lost and you cannot delete the
> object.[/color]

hmm.. Thanks for the suggestion, I will ponder over it :)

Closed Thread