C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Passing dereferenced new pointer to reference paramter

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Sat Oct 28, 2006 9:10 am    Post subject: Passing dereferenced new pointer to reference paramter Reply with quote



I'm starting out with c++ and for some reason I cant get my brain
around this one:

If I have the following:

void Foo (someClass& x)
{}

int Main (void)
{
Foo(*(new someClass));
}


Is the memory allocated for the 'new someClass' that is passed to Foo,
automatically deleted, or must it be deleted using 'delete'? If so, how
given that the new someClass is never actually assigned to a pointer?
Or, is it assigned to x... i'm not sure....i think i might be missing
something here in the way c++ treats this kind of thing.

Thanks
Back to top
Alf P. Steinbach
Guest





PostPosted: Sat Oct 28, 2006 9:10 am    Post subject: Re: Passing dereferenced new pointer to reference paramter Reply with quote



* shanemh (AT) gmail (DOT) com:
Quote:
I'm starting out with c++ and for some reason I cant get my brain
around this one:

If I have the following:

void Foo (someClass& x)
{}

int Main (void)

Note 1: C++ is a case-sensitive language; that should be 'main'.
Note 2: C++ is not C, using 'void' there is a C'ism best a-voided.
Note 3: When posting code, copy and paste code that compiles.

Quote:
{
Foo(*(new someClass));
}


Is the memory allocated for the 'new someClass' that is passed to Foo,
automatically deleted

Not during program execution.


Quote:
, or must it be deleted using 'delete'? If so, how
given that the new someClass is never actually assigned to a pointer?

Do this:

int main()
{
someClass o;
Foo( o );
}

Forget about pointers and 'new' until you have mastered local objects
and standard library container classes.


Quote:
Or, is it assigned to x... i'm not sure....i think i might be missing
something here in the way c++ treats this kind of thing.

You do, yes (see above).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.