简体   繁体   中英

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();
} // will f go out of scope when this function returns?

void DoSomething2(File* f);

Two questions:

  1. As seen in the comment, will f go out of scope when the function returns?
  2. Do you suggest writing function using reference or pointer? (I'm talking about private functions)
  1. f the reference local to DoSomething will go out of scope, but this obviously has no consequences.

    The f object local to main goes out of scope only after the end of the main (which, incidentally, should be main . main之后才超出范围。

    To sum it up, references are aliases to objects, but the original objects retain their scope, as happens with pointers.

  2. 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 eg when I want that parameter to be optional 1 (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.


  1. 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, ie, 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 (ie, 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM