Wrox Programmer Forums
|
BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5
This is the forum to discuss the Wrox book Ivor Horton's Beginning Visual C++ 2008 by Ivor Horton; ISBN: 9780470225905
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Ivor Horton's Beginning Visual C++ 2008 ISBN: 978-0-470-22590-5 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 29th, 2008, 04:22 AM
Authorized User
 
Join Date: Nov 2008
Posts: 15
Thanks: 1
Thanked 1 Time in 1 Post
Default Returning Automatic Variables from Functions

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.
 
Old November 29th, 2008, 07:32 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

Even without seeing the code for class CRect, I would guess that it has (at a minimum) a copy constructor and so, yes, your assumption that a copy of the local variable is returned would be correct.

Actually, you can be pretty sure it has both a copy contstructor and an assignment operator (that is, operator =), since we see the line
    BoundingRect = m_EnclosingRect;
That almost surely is doing a member-wise copy of the data members of the m_EnclosingRect into the corresponding members of BoundingRect.

My C++ is a little rusty (it's been 8 years since I really used C++), but I vaguely recall that if you have an operator = defined for a class, then C++ will automatically use that to create a copy constructor for you. (And of course the default copy or assignment is just a bit-wise copy of all bytes, but that's often not the semantics you want.)

Not germaine to this question, but you can always return primitive values (e.g., int, float, etc.) from functions, however they are declared, because all primitives have implicit copy constructor semantics.

Incidentally, if a class purposely does *NOT* have an operator = or a copy constructor, then no, you wouldn't be able to return an instance of it from a function by returning an automatic variable.

Let's see how much the people who are "up" on C++ tear up my answer.
The Following User Says Thank You to Old Pedant For This Useful Post:
jabney (December 16th, 2008)
 
Old November 29th, 2008, 09:56 PM
Authorized User
 
Join Date: Nov 2008
Posts: 15
Thanks: 1
Thanked 1 Time in 1 Post
Default

OP, thanks, that helps a lot. It makes perfect sense, I just needed to wrap my head around it and flush out the incorrect assumptions. I learn best visually, so descriptions of things don't always stick to my brain very well until I've come up against the concept in practice.

BTW, I think I remember Ivor mentioning that a class's copy constructors and assignment operators, if they're not explicitly defined, are generated by the compiler by default to create member-wise clones.

This would mean that any class without a copy constructor or assignment operator would get one anyway, which is why you want to create them manually in any situations where class members are assigned dynamic memory -- unless you actually wanted object instances to share the same memory for some reason.

Thanks again!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Can variables be pulled out of functions Apocolypse2005 Javascript How-To 3 July 21st, 2006 05:16 PM
Help with functions and variables DeadlyDesigns.NET Beginning PHP 6 January 26th, 2005 12:28 AM
Functions and variables collie VB.NET 2002/2003 Basics 3 February 5th, 2004 02:45 AM
Global variables and functions madhukp ASP.NET 1.x and 2.0 Application Design 3 October 9th, 2003 09:57 AM
Returning variables from a function starsol Beginning PHP 1 September 11th, 2003 12:58 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.