C ++ Primer study note _ 17 _ class and data abstraction (3) _ class scope, primer_17

Source: Internet
Author: User

C ++ Primer study note _ 17 _ class and data abstraction (3) _ class scope, primer_17

C ++ Primer study note _ 17 _ class and data abstraction (3) _ class scope

Introduction:

Each class defines its own new scope and unique type. Even if the two classes have identical member lists, they are of different types.The members of each class are different from those of any other class (or any other scope )..

 

I. Search for names in a class scope

1) First, find the name declaration in the block using the name. Only the name declared before this option is used.

2) If no value is found in 1), it will be found in the surrounding scope.

If no declaration is found, an error occurs.

 

The class definition is actually processed in two phases:

1) First, the compiler declares;

2) Compile the definition only when all the Members appear.

 

The identifiers described in the class scope are only visible in the class. In addition to class scopes, there are also block scopes, file scopes, function prototype scopes, and function scopes. For example:

# Include <iostream> using namespacestd; class Test {public: int num ;}; // num = 20; Error, the scope of num is within the class int num = 20; // The scope of num is the file scope. It is different from the scope of num in the class. int add (int a, int B); //, B The scope of the two identifiers is the function prototype scope int main (void) {int num = 30; // num is the block-Based Domain {int num = 100; // num is the block-Based Domain} cout <num <endl; cout <: num <endl; return 0;} int add (int a, int B) // The parameters a and B are also block scopes {return a + B ;}


Int test () {LABEL1: // function scope cout <"label1" <endl; goto LABEL3; LABEL2: cout <"label2" <endl; goto LABEL1; LABEL3: cout <"label3" <endl; goto LABEL2 ;}


1. Search for the name of the class member declaration

You must first define the type names in the class to use them as the data member type, or the return type or parameter type of the member function. Once a name is used as a type name, it cannot be repeatedly defined:

typedef double Money;class Account{public:   Money balance()    {       return bal;    } private:   typedef long double Money; //Error   Money bal;};

2. Search for the names of the specified class members

1) First, check the declaration in the partial scope of the member function.

2) If the declaration of this name cannot be found in the member function, check the declaration of all class members.

3) if the declaration of this name cannot be found in the class, check the declaration in the scope before the member function definition.

 

3. Class Members follow the regular block scope name search

Do not use parameters and members with the same name in the program:

Int height; class Screen {public: typedef int index; // Since the height is defined in the function parameters, // Therefore, the height parameter will block void dummy_fcn (index height) {cursor = width * height;} private: index cursor; index height, width ;};

Although the class member is blocked, you can use the class name to limit the member name or explicitly use the this pointer to use it:

Void dummy_fcn (index height) {cursor = width * this-> height; // the two statements share the same role as cursor = width * Screen: height ;}

4. Search for the function scope in the class scope

If you want to use the height member, it is best to define a different name for the parameter:

    void dummy_fcn(index ht)    {        cursor = width * height;    }

Although height is first used in dummy_fcn and then declared, the compiler still determines that the data member named height is used here.

 

5. Search for the class scope in the peripheral Scope

Although the Global Object height is blocked by the data member height in the class, you can use the global scope operator to limit the name. You can still use it:

   void dummy_fcn(index height)    {       cursor = width * ::height    }

6. nested and local classes

(1) Nested classes

The nested class object must be used as the underlying implementation of the peripheral class. The nested class is only used for the implementation of the peripheral class and can be hidden from the user.

From the scope perspective, the nested class is hidden in the peripheral class, and the class name can only be used in the peripheral class. If the class name is used outside the scope of the peripheral class, the name must be specified.

Member functions in a nested class can be defined in the external body of the class.

The member functions of the nested class have no access to the private members of the peripheral class, and vice versa.

Nested classes are just syntactically embedded.


(2) Local class

Classes can also be defined in the function body. Such classes are called local classes ). The local class is visible only in the defined local domain.

The member functions of a local class must be defined in the class body.

There cannot be static members in a local class. We will discuss the static members and static member functions in the class later.

# Include <iostream> using namespace std; class Outer {public: class Inner {public: void Fun (); // {// cout <"Inner: Fun... "<endl; //}; public: Inner obj; void Fun () {cout <" Outer: Fun... "<endl; obj. fun () ;}}; void Outer: Inner: Fun () {cout <"Inner: Fun... "<endl;} void Fun () {class LocalClass {public: int num _; void Init (int num) {num _ = num;} void Display () {cout <"num =" <num _ <endl;} // static int num2 _; // static members cannot be defined inside the local class}; LocalClass lc; lc. init (10); lc. display () ;}int main (void) {Outer o; o. fun (); Outer: Inner I; I. fun (); // LocalClass lc; Error. The local class can only use return 0 in the function body that defines it ;}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.