Understanding C++ Scope and Namespace

Preface

Every variable, function, array, structure, class, and other entities have their own valid scope, which is the scope of the variable.

The namespace is a user-defined scope introduced in ANSI C++ to handle common naming conflicts in programs.

Understanding C++ Scope and Namespace

Meet/Yourself

Understanding C++ Scope and Namespace

C++ Scope

Every variable, function, array, structure, class, and other entities have their own valid scope, which is the scope of the variable.

Understanding C++ Scope and Namespace

1. Local Variable

A variable defined inside a function is called a local variable, and its scope is limited to that function. Once outside that function, it becomes invalid, and using it will result in an error.

For example:

Understanding C++ Scope and Namespace

2. Global Variable

A variable defined outside all functions is called a global variable, and its scope is the entire program, including all source files, such as .cpp and .h files.

For example:

Understanding C++ Scope and Namespace

3. Difference Between Local and Global Variables

1) A local variable is declared within a block or function. The scope of a local variable is limited to that block or function. If a local variable is not initialized, it will contain garbage data.

2) A global variable is declared before all blocks and functions. A global variable is valid for all functions declared after it. Global variables are initialized with a default value, such as 0.

Note: (If you want to call a global variable from another file, if you declare another global variable with the same name, the compiler will report an error due to the name conflict. In this case, you should use the extern variable. The extern declaration tells the compiler that the definition of this variable is in another file, so it will not allocate memory for it).

4. Difference Between Static Local Variables and Static Global Variables

1) The scope of a non-static global variable is the entire source program. When a source program consists of multiple source files, a non-static global variable is valid in each source file.

2) A static global variable limits its scope, meaning it is only valid within the source file that defines the variable and cannot be used in other source files of the same source program.

C++ Namespace

The namespace is a user-defined scope introduced in ANSI C++ to handle common naming conflicts in programs. Multiple disjoint scopes can be created to separate entities to avoid name conflicts.

1) Accessing entities within the scope of a namespace

Understanding C++ Scope and Namespace

2) Standard Namespace std

All identifiers in the C++ standard library are defined in a namespace called std, or in other words, the functions, classes, and object templates in standard header files are defined in the namespace std. You can add using namespace std; at the beginning of the file to avoid writing std:: when using them.

Example of usage:

Understanding C++ Scope and Namespace

Understanding C++ Scope and NamespaceUnderstanding C++ Scope and Namespace

*Declaration:This article is compiled from the internet, and the copyright belongs to the original author. If there is any incorrect information or infringement of rights, please contact us for deletion or authorization matters.

Leave a Comment