I have just a bit of minor confusion.
I was under the impression that
any variable that was created within the body of a function would go out of scope after the function returned.
Specifically in Chapter 5 pages 265 and 268, we are warned:
- Never ever return the address of a local automatic variable from a function.
Never ever return a reference to a local variable from a function.
I've been operating under the impression that automatics simply shouldn't ever be returned from functions, but apparently this only applies to
pointers and references to automatics.
In chapter 14 we're told to build the GetBoundRect() function like this:
Code:
CRect CElement::GetBoundRect()
{
CRect BoundingRect;
BoundingRect = m_EnclosingRect;
BoundingRect.InflateRect(m_Pen, m_Pen);
return BoundingRect;
}
So here were returning an automatic from a function and there's no problem. The only thing I can figure is that the automatic variable
BoundingRect is copied as it's being returned, and that the automatic within the function body really does go out of scope.
Is this correct, or could someone give me a better idea if how this is working? I suppose I'm answering my own question. An automatic can be returned from a function, as long as it's not a pointer or a reference.