Will a variable go out of scope when the function returns?
void main()
{
File f;
DoSomething(f);
DoSomething2(&f);
}
void DoSomething(File& f)
{
f.Process();
} // wil开发者_JAVA技巧l f go out of scope when this function returns?
void DoSomething2(File* f);
Two questions:
- As seen in the comment, will f go out of scope when the function returns?
- Do you suggest writing function using reference or pointer? (I'm talking about private functions)
f
the reference local toDoSomething
will go out of scope, but this obviously has no consequences.The
f
object local tomain
goes out of scope only after the end of themain
(which, incidentally, should beint
main
.To sum it up, references are aliases to objects, but the original objects retain their scope, as happens with pointers.
A common suggestion is use references when you can, pointers when you have to.
In general I use references whenever I need a parameter to be passed - duh - for reference, and pointers e.g. when I want that parameter to be optional1 (pointers can be
NULL
), when I'm accepting arrays, ... and in general, I'd say, when in the function I'm going to use the pointer as a pointer, not dereferencing it all the time.
- although overloads and default values can be better than a
NULL
able parameter in many situations.
f
will not fall out of scope because it is an argument to the function, i.e., it was not created inside of the function.
It is preferable to take a reference if possible as it is guaranteed to be a valid reference and not null (yes, it can be null if you use some trickery and invoke undefined behavior, but then your program isn't valid anyway).
Scope is a compile-time concept, not a run-time concept. It refers to the visibility of names, not the lifetime of objects.
You have three things named f
. One of them is local to main
; the others are local to DoSomething
and DoSomething2
, respectively. For the first two, the scope (of the name, not of the object) extends to the closing }
.
The lifetime of the object named f
that's defined in main
extends until main
returns. That's the only File
object in the code you've shown us.
(And unless you're using a freestanding (i.e., embedded) implementation, it's int main()
, not void main()
. Complain to whomever or whatever taught you that void main()
is correct.)
Of course not, f
's scope is the function main
. It will get destroyed when main
returns (ie when your program exits).
As to your second question, references are always better (insert grain of salt here), you are guaranteed you're never going to get a null
and the syntax is cleaner (opinion, I know).
When you call your "DoSomething" function, the do something function takes a reference of the f that's called from main. This means that the scope of the f in main is defined from the beginning to the end of main. The "f.process", by nature, does not go out of scope. It stays in the scope defined in main.
Question 1: you are passing the address of a file and giving it the name f (whose scope is only the function DoSomething(&f). Calling a member function of the File object will never affect the scope of the f variable.
Sort Answer = no.
Question 2: references from my understanding is a Java concept, passing by "reference" in C++ is done by sending an object pointer as an argument. Whether or not passing by a pointer is the best solution largely depends on the context of what you are doing.
Sort Answer = maybe?
精彩评论