Chapter 5th sharing and protection of data

Source: Internet
Author: User

It mainly introduces the concept of scope, visibility and life cycle of identifiers, as well as the sharing and protection of class members.

5.1 Scope and visibility of identifiers

5.1.1 Scope

A scope is an area in which an identifier is valid in the body of the program.

1. Function prototype scope:

When a function prototype is declared, the scope of the formal parameter is the function prototype scope, and the formal parameter identifier can be omitted. However, considering the readability of the program, it is often necessary to give the formal parameter identifier when the function prototype is declared.

2. Local scope:

The scope of a parameter in a function parameter list, starting at the declaration in the formal parameter list, to the end of the entire function body. A variable declared in the body of a function whose scope begins at the declaration, until the closing brace of the block where the declaration resides. A variable with local scope is also known as a local variable.

3. Class Scope

(1) If a local scope identifier with the same name is not declared in the member function of the class, the member M can be accessed directly within the function

(2) The basic method of accessing an object member in a program: object name, reference name, static member access, pointer.

4. Namespace scope

(1) Definition: namespace namespace name {namespace of various declarations ... }

(2) using: namespace name:: identifier name (specified identifier within specified namespace exposed)/using NAMESAPCE namespace name (all identifiers within the specified namespace are exposed to the current scope)

(3) Global namespace: The default namespace, in which identifiers declared outside the declared namespace of the display are in one global namespace.

(4) Anonymous namespaces: Do not specify the name of the namespace, commonly used to mask identifiers that you do not want to expose to other source files

(5) A variable with a namespace scope becomes a global variable

5.1.2 Visibility

A program runs to a point where the identifier that can be referenced is the identifier that is visible at that place

Half the rules for scope visibility:

(1) The declaration of the identifier is in front of the reference after

(2) An identifier with the same name cannot be declared in the same scope

(3) identifiers with the same name in different scopes that do not have a mutually inclusive relationship do not affect each other

(4) If an identifier of the same name is declared in two or more scopes with a containing relationship, the outer identifier is not visible in the inner layer

5.2 Lifetime of the object

The time of the object from birth to end is its lifetime. The lifetime of an object is divided into two types, dynamic lifetime and static life time.

5.2.1 Static Life Time

1. Concept: If the lifetime of an object is the same as the run time of the program, it is said to have a static lifetime

2. Feature: does not produce a copy with each function call, and does not disappear with function return

5.2.2 Dynamic Lifetime

Objects declared in a local scope with dynamic lifetimes, which are customarily referred to as local lifetime objects, are created and declared, ending when the block where the declaration is executed is completed.

Static members of Class 5.3

5.3.1 Static data members

1. A class property is a data item that describes the common characteristics of all the objects of a class, and its property values are the same for any instance of the object

2. Static data members have a static lifetime, which can be obtained by type: Class Name:: identifier

3. Half defined as private members

5.3.2 Static member functions

Static member functions can directly access the static data and member functions of the class object, and access to non-static members must pass the object name

Friend of Class 5.4

The friend function provides a mechanism for sharing data between a class member function and a general function between different classes or object member functions.

5.4.1 friend function

A friend function is a non-member function that is decorated with a keyword friend in a class, in which the private and protected members of a class can be accessed through the object name in its function body

5.4.2 Friend class

If Class A is a friend of Class B, all member functions of Class A are friend functions of Class B and can be private classes and protected members of B.

5.4.3 Note:

1. Friend relationships cannot be passed

2. A friend relationship is one-way

3. Friend relationships cannot be inherited

5.5 Protection of shared data

5.5.1 Constant object: Must be initialized and cannot be updated. You can only invoke the constant member function.

5.5.3 class members decorated with const

1. Regular member functions:

Type specifier function name (parameter list) const;

A constant member function cannot call a very member function, and a very member function can call a constant member function

2. Regular data members:

The initialization list is initialized in the destructor, not the initial pass in the way that the parameter is passed, because it is a constant

3. Frequently quoted: The object referenced by the constant reference cannot be updated, analogy: const int *P, int * Const P

Const type Descriptor & Reference name

5.6 Multi-file structure and compilation preprocessing commands

5.6.1 General Organizational structure of C + + programs:

1. class definition file. h

2. class implementation file. cpp

3. Classes use files. cpp

5.6.2 external variables and external functions

1. External variables: extern keyword decoration

2. External functions: Decorated with extern keyword, can be omitted

3. Restricting variables and functions within the compilation unit: using the namespace keyword

5.6.3 standard C + + library

Logically divided into 6 categories: input and Output class, container class and ADT, storage management class----Error handling--run environment support

5.6.4 compilation preprocessing

All preprocessor directives are guided by "#", each preprocessing instruction takes a single line, do not end with a semicolon, can appear in any position

1. #include指令: A file contains an instruction to embed another source file at that point in the current source file, usually to embed a header file

(1) #include <>: Search by standard, files are in the include subdirectory of the system directory

(2) #include "": First search in the current directory, if not, then search by standard way

2. #define: Defining macros

#undef: Deleting a macro

3. Conditional compilation directives

#if ... #elif ... #elif ... #else ... #endif

4. #defined操作符: Do not start with # defined (identifier): returns 0 if undefined, otherwise returns non 0

Chapter 5th sharing and protection of data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.