C++ lecture 11

17 8 0
C++ lecture 11

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

C++ Programming Lecture 11 Functions – Part III The Hashemite University Computer Engineering Department (Adapted from the textbook slides) Outline  Introduction  Identifiers storage classes  Identifiers scope rules The Hashemite University Introduction  The main attributes attached with any variable or identifier include:   Name, type, size, value (as taken before) Storage class   Scope   Determines the period during which the variable exists in memory Where the identifier can be referenced in program Linkage   Where an identifier is known in multiple source file programs Determine whether an identifier is known only in the current source file or in any source file The Hashemite University Storage Classes  Storage classes types:    Automatic storage Static storage Five storage-class specifiers (or keywords used in C++):      auto, register, extern, static mutable, Automatic storage Static storage The Hashemite University Automatic Storage    Variables created and destroyed within its block (created when entering the block and destroyed when leaving this block) Can only be used with local variables and parameters of a specified function Two types:  auto    Default for local variables Example: auto float x, y; register    Tries to put variables into high-speed registers The compiler can ignore it if no sufficient number of registers are available Not necessary in optimized compilers The Hashemite University Static Storage     Variables exist from the point of declaration for entire program execution Static variables are created and initialized once when the program begins execution Numeric variables are initialized to by default unless the programmer initialize them explicitly Two types:  static  Usually, used with local variables defined in functions  Permanent storage for the static variable is allocated, so that it is not removed from memory when leaving the block it is defined on  Keep value after function ends  Only known in their own function (known inside its function like auto and register but it is not removed from memory when the function exit)  extern  Default for global variables and functions  used to specify that the variable is declared in a different file  Known in any function The Hashemite University Storage Classes Notes     Do not use multiple storage specifiers at the same time  syntax error E.g.: register auto double x = 0; // syntax error mutable storage class is used with classes (in the object oriented classes) Mutable keyword can only be applied to non-static class members The Hashemite University Identifier Scope Rules I  Scope is where the identifier can be referenced in program (The portion of the program where an identifier can be used)  Scope Types:  File scope:     Defined outside a function, known in all functions Known in all functions from the point at which the identifier is declared until the end of the file Examples include, global variables, function definitions and functions prototypes Function scope:  Can only be referenced inside a function body The Hashemite University Identifier Scope Rules II  Block scope:     Function prototype scope:    Declared inside a block Begins at declaration, ends at } Includes variables, function parameters (local variables of function) If two nested blocks have the same variable, outer blocks “hidden” from inner blocks Identifiers in parameter list Names in function prototype optional, and can be used anywhere Class and namespace scopes: will be taken later The Hashemite University Summary Global variables are by default static Local variable are by default auto You cant reference (print/modify) local variable outside its scope Examples bool fun() { static int var=-1; //line } int main() { cout

Ngày đăng: 12/10/2021, 21:08

Mục lục

    C++ Programming Lecture 11 Functions – Part III

    Storage Classes -- Notes

    Identifier Scope Rules I

    Identifier Scope Rules II

    Unary Scope Resolution Operator

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan