What does scope mean?

What does scope mean?

I understand the scope of the variables. When we talk about the scale of functions, do they refer to functions inside structures (classes) or is there an area for ordinary functions that we call in main() a C / C ++ program?

+6
source share
4 answers

Functions can have a global, namespace, class (usually called members in this case), or local (within another function) scope. They can also be static , giving them an internal link or within an anonymous namespace, making them inaccessible outside the translation unit (while still having an external link so that they can be used as template parameters).

+6
source

In short, a scope is an area in which names can be declared. Names declared in scope are available within that scope and, in some cases, also externally.

(To be pedantically accurate, this is actually the scope of the declaration, and the scope of the name is part of the program in which the name is valid. It starts where it is declared and includes the rest of this region and, sometimes, in some other regions.)

The scope is represented by namespaces, classes, and compound statements (that is, blocks of code statements surrounded by {} ). The latter includes body functions.

Most objects and functions have names, and each of these names is inside a scope.

Thus, the "scope of a function" can mean two things: either the area defined by the body of the function in which its local variables are declared; or the scope (class or namespace) in which the function name is declared.

UPDATE: you say you mean the scope of the function name. It always starts immediately after the announcement; where it ends depends on where this announcement was.

  • If it is declared inside a namespace, it lasts until that namespace is closed. If the namespace is reopened in the same translation unit, it returns to scope.
  • If it is declared inside the class definition as a member function, then the scope continues until the end of the class definition. It also enters into the scope within the definitions of any derived classes, as well as within the definitions of a member of this class or derived classes.
  • If it is declared inside the class definition as a friend or inside the function definition, then the name is actually declared in the surrounding namespace, and the scope coincides with this case.
+5
source

Yes, functions also have scope, although their scope is usually larger than most variables.

In C [edit: which was one of the tags when I wrote this), functions have either a global scope or scope. The global scope refers to the normal function visible throughout the program. The scope of the file refers to the function that you marked as "static", so it is visible only within the same translation unit.

C ++ uses several different names for these, but has the same basic concepts. It adds namespaces as things actually called "namespaces" and struct s / class es. With one exception, a function in the namespace is visible only inside this namespace. An exception is the definition of the friend function inside class / struct :

 class X { friend void whatever(X const &) { do_something(); } }; 

In this case, although the function is defined inside X , its name is entered into the surrounding namespace so that it appears outside X.

+2
source

I think this implies an area in which labels (A Label is what you use with goto ) are visible.

Perhaps this article can also help you understand areas. You can also consider this question about stackoverflow.

0
source

Source: https://habr.com/ru/post/919760/


All Articles