 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Guest
|
Posted: Sat Oct 28, 2006 9:10 am Post subject: Passing dereferenced new pointer to reference paramter |
|
|
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
|
Posted: Sat Oct 28, 2006 9:10 am Post subject: Re: Passing dereferenced new pointer to reference paramter |
|
|
* 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 |
|
 |
|
|
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
|
|