C++ | Scope Rules

In this article, we will be learning about the scope rule i.e., rules to tell if a variable, parameter, and function is visible or accessible at certain places. Thus places, where an entity can be accessed or visible, are referred to as the scope of that entity.

When we declare a program element such as a class, function, or variable, its name can only be “seen” and used in certain parts of our program. The context in which a name is visible is called its scope. For example, if we declare a variable x within a function, x is only visible within that function body. It has local scope. We may have other variables by the same name in our program; as long as they are in different scopes, they do not violate the One Definition Rule and no error is raised. For automatic non-static variables, the scope also determines when they are created and destroyed in program memory.

There are a total of six kinds of scope:

  • Global scope
  • Namespace scope
  • Local scope
  • Class scope
  • Statement scope
  • Function scope

Global Scope

A global name is one that is declared outside of any class, function, or namespace. However, in C++ even these names exist with an implicit global namespace. The scope of global names extends from the point of declaration to the end of the file in which they are declared. For global names, visibility is also governed by the rules of the linkage which determine whether the name is visible in other files in the program.

See also  C++ Vectors

Example:

Output:

5
10

Namespace Scope

A name that is declared within a namespace, outside of any class or enum definition or function block, is visible from its point of declaration to the end of the namespace. A namespace may be defined in multiple blocks across different files.

Example:

There are three ways that code outside the namespace can access their members.

  1. Using the fully qualified name:

2. Using declaration to bring a specific variable into scope:

3. Using directive to bring everything in the namespace into scope:

Local Scope

A name declared within a function or lambda, including the parameter names, have local scope. They are often referred to as “locals”. They are only visible from their point of declaration to the end of the function or lambda body. Local scope is a kind of block scope, which is discussed later in this article.

Example:

Output:


Error: age was not declared in this scope

Class Scope

Names of class members have the class scope, which extends throughout the class definition regardless of the point of declaration. Class member accessibility is further controlled by the public, private, and protected keywords. Public or protected members can be accessed only by using the member-selection operators (. or ->) or pointer-to-member operators (.* or ->*).

Example:

Statement Scope

Names declared in a for, if, while, or switch statement are visible until the end of the statement block.

Function Scope

A label has function scope, which means it is visible throughout a function body even before its point of declaration. Function scope makes it possible to write statements like goto cleanup before the cleanup label is declared.

See also  C++ Priority Queue

Example:

That is all for this article. Happy Coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

we provide projects, courses, and other stuff for free. in order for running we use Google ads to make revenue. please disable adblocker to support us.