Describes the differences between C ++ local classes and C ++ Nested classes.

Source: Internet
Author: User

If the relationship between the nested type and its external type requires the accessible semantics of members, the C ++ nested class is required. The nested type should not execute tasks for types other than its declared type, C ++ local classes allow classes, structures, and interfaces to be divided into multiple small blocks and stored in different source files. This implementation is easy to develop and maintain.

C ++ local class:
Classes defined in a function are called local classes. In a local class, only objects and functions in its peripheral scope can be associated, because the variables in the peripheral scope are irrelevant to the objects in the department class. Local classes cannot be inherited by external entities. When defining a local class, note that static member functions cannot be described in the local class, and all member functions must be defined in the class body. In practice, local classes are rarely used. The following is an example of a local class.

 
 
  1. Int;
  2. Void fun ()
  3. {
  4. Static int s;
  5. Class
  6. {
  7. Public:
  8. Void init (int I ){S=I;}
  9. };
  10. A m;
  11. M. init (10 );
  12. }
  13.  
  14.  
  15. Another use of local classes is to implement type conversion. The following code:
  16.  
  17. Class Interface
  18. {
  19. Public:
  20. Virtual void Fun () = 0;
  21. };
  22.  
  23. Template<ClassT, class P> 
  24. Interface * MakeAdapter (const T & obj, const P & arg)
  25. {
  26. Int x;
  27. Class Local: public Interface
  28. {
  29. Public:
  30. Local (const T & obj, const P & arg)
  31. : Obj _ (obj), arg _ (arg ){}
  32. Virtual void Fun ()
  33. {
  34. X=100;
  35. Obj _. Call (arg _);
  36. }
  37. Private:
  38. T obj _;
  39. P arg _;
  40. };
  41. Return new Local (obj, arg );
  42. }

C ++ nested class:
The class defined in a class is called the C ++ nested class, and the class defining the nested class is called the peripheral class. The purpose of defining a nested class is to hide the class name and reduce the global identifier, so as to limit whether users can use this class to create objects. This improves the class abstraction capability and emphasizes the master-slave relationship between two classes (peripheral class and nested class. The following is an example of a nested class:

 
 
  1.  class A  
  2.  {  
  3.  public:  
  4. class B  
  5.  {  
  6. public:  
  7. …  
  8. private:  
  9. …  
  10. };  
  11. void f();  
  12. private:  
  13. int a;  


Several descriptions of Nested classes:
1. From the perspective of scope, the nested class is hidden in the peripheral class, and the class name can only be used in the peripheral class. If you use this class name within the scope of the peripheral class, you need to apply a name restriction.

2. From the perspective of access permission, the nested class name has the same access permission rules as the object member name of its peripheral class. You cannot access private member functions of nested class objects or create objects for nested classes in the private section of the peripheral class.

3. The member functions in the C ++ nested class can be defined in the external body of the class.

4. The members in the nested class are not members of objects in the peripheral class, and vice versa. The member functions of the nested class have no access to the members of the peripheral class, and vice versa. When analyzing the access relationships between Nested classes and peripheral classes, the nested classes are often treated as non-nested classes. In this way, the preceding Nested classes can be written in the following format:

 
 
  1. class A  
  2. {  
  3. public:  
  4. void f();  
  5. private:  
  6. int a;  
  7. };  
  8.  
  9. class B  
  10. {  
  11. public:  
  12. …  
  13. private:  
  14. …  
  15. }; 

As you can see, nested classes are only syntactically embedded.

5. The friends stated in the C ++ nested class do not have access to the members of the peripheral class.

6. If the nested class is complex, you can only describe the nested class in the peripheral class. The detailed content about nesting can be defined in the file domain of the peripheral class in vitro.

  1. Differences between standard input implementation methods in C and C ++
  2. How does the C ++ compiler allocate storage space for Const constants?
  3. Basic Conception and method of C ++ Class Library Design
  4. Several Methods for converting C ++ Language
  5. How to better compile C ++ code

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.