Instance creation

D

dave1997_us

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
 
R

Rolf Magnus

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() ;

What is an "o/p"?
In the second case, how is the object created

Just the same as in the first case. Dynamically with new.
and what instance of the object do we have to "delete" at the end of the
program.

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.
 
D

dave1997_us

o/p as in output.
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.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,237
Latest member
AvivMNS

Latest Threads

Top