Thinking in c volume 1 - 2nd edition - phần 3 docx

88 250 0
Thinking in c volume 1 - 2nd edition - phần 3 docx

Đ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

// int scp2; // scp2 visible here // { // scp1 & scp2 still visible here // int scp3; // scp1, scp2 & scp3 visible here // } // < scp3 destroyed here // scp3 not available here // scp1 & scp2 still visible here // } // < scp2 destroyed here // scp3 & scp2 not available here // scp1 still visible here // } // < scp1 destroyed here ///:~ The example above shows when variables are visible and when they are unavailable (that is, when they go out of scope) A variable can be used only when inside its scope Scopes can be nested, indicated by matched pairs of braces inside other matched pairs of braces Nesting means that you can access a variable in a scope that encloses the scope you are in In the example above, the variable scp1 is available inside all of the other scopes, while scp3 is available only in the innermost scope Defining variables on the fly As noted earlier in this chapter, there is a significant difference between C and C++ when defining variables Both languages require that variables be defined before they are used, but C (and many other traditional procedural languages) forces you to define all the variables at the beginning of a scope, so that when the compiler creates a block it can allocate space for those variables While reading C code, a block of variable definitions is usually the first thing you see when entering a scope Declaring all variables at 156 Thinking in C++ www.BruceEckel.com the beginning of the block requires the programmer to write in a particular way because of the implementation details of the language Most people don’t know all the variables they are going to use before they write the code, so they must keep jumping back to the beginning of the block to insert new variables, which is awkward and causes errors These variable definitions don’t usually mean much to the reader, and they actually tend to be confusing because they appear apart from the context in which they are used C++ (not C) allows you to define variables anywhere in a scope, so you can define a variable right before you use it In addition, you can initialize the variable at the point you define it, which prevents a certain class of errors Defining variables this way makes the code much easier to write and reduces the errors you get from being forced to jump back and forth within a scope It makes the code easier to understand because you see a variable defined in the context of its use This is especially important when you are defining and initializing a variable at the same time – you can see the meaning of the initialization value by the way the variable is used You can also define variables inside the control expressions of for loops and while loops, inside the conditional of an if statement, and inside the selector statement of a switch Here’s an example showing on-the-fly variable definitions: //: C03:OnTheFly.cpp // On-the-fly variable definitions #include using namespace std; int main() { // { // Begin a new scope int q = 0; // C requires definitions here // // Define at point of use: for(int i = 0; i < 100; i++) { 3: The C in C++ 157 q++; // q comes from a larger scope // Definition at the end of the scope: int p = 12; } int p = 1; // A different p } // End scope containing q & outer p cout

Ngày đăng: 13/08/2014, 09:20

Từ khóa liên quan

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

Tài liệu liên quan