Re: Is this reference declaration legal?
jl_post@hotmail.com wrote:
I've wondered for quite some time if it's legal to make a reference
to a pointer, like this:
void f()
{
SomeClass *ptr = new SomeClass;
SomeClass & ref = *ptr; // is this legal?
Yes, certainly.
// Do stuff with ref here.
delete(ptr);
Actually 'delete' is an operator, not a function. The parens are
superfluous.
ptr = NULL;
There is really no need for that.
return;
}
Ordinarily I'd say this is legal, but I haven't read any
documentation that explicitly says so.
(Now, I understand that it's bad form to allocate memory and free/
delete it in the very same scope,
Why?
> but I just want to know if it's okay
to declare a reference to a (de-referenced) pointer, assuming the
pointer points to a valid object and that the reference is not used
after the memory is freed/deleted.)
Yes, it is.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask