38. c ++ primer 4th notes, special tools and technologies, nested classes

Source: Internet
Author: User

1Define another class within a class. Such a class is called a nested class (Nested class), Also known as the nested type (Nested type). Nested classes are most commonly used in execution classes.

Nested classes are independent classes, which are basically irrelevant to their peripheral classes. Therefore, the peripheral classes and nested class objects are independent of each other. Objects of the nested type do not have the Members defined by the peripheral class. Likewise, the members of the peripheral class do not have the Members defined by the nested class.

ExampleCode

 
Template <class type> class queue {// interface functions to queue are unchangedprivate: // public members are OK: queueitem is a private member of queue // only queue and its friends may access the members of queueitemstruct queueitem {queueitem (const type &); Type item; // value stored in this elementqueueitem * Next; // pointer to next element in the queue}; queueitem * head; // pointer to first element in queuequeueitem * tail; // pointer to last element in queue}; Template <class type> queue <type >:: queueitem: queueitem (const type & T): item (t ), next (0 ){}

2The class nested in the class template is the template. The ing between the instantiation of nested class templates and the instantiation of peripheral class templates is one-to-one.

The nested class members in the external definition of the class must be defined in the same scope of the defined peripheral class. A member of a nested class defined outside the base class cannot be defined inside the peripheral class. The member of the nested class is not a member of the peripheral class.

Sample Code

#include 
  
    using namespace STD; template 
   
     class queue {// interface functions to queue are unchangedpublic: void POP (); Private: // public members are OK: queueitem is a private member of queue // only queue and its friends may access the members of queueitemstruct queueitem; queueitem * head; // pointer to first element in queuequeueitem * tail; // pointer to last element in queue}; Template 
    
      struct queue 
     
      :: queueitem {queueitem (const type &): item (t), next (0) {}; type item; // value stored in this elementqueueitem * Next; // pointer to next element in the queuestatic int static_mem ;}; template 
      
        int queue 
       
        :: queueitem: static_mem = 1024; Template 
        
          void queue 
         
          :: POP () {queueitem * P = head; head = head-> next; Delete P;} int main () {return 1 ;}
         
        
       
      
     
    
   
  

The name of the READ function from right to left.

3Just as we can define the members of The nested class externally in the class definition body, we can also define the entire nested class externally in the peripheral class definition body.

4If the nested class declares a static member, its definition also needs to be placed in the outer scope.

5The objects in the peripheral scope are not associated with nested objects.

Non-static functions in nested classes have implicitThisPointer to an object of the nested type. An object of the nested type only contains members of the nested type and cannot be used.ThisPointer to obtain the members of the peripheral class. Similarly, non-static member functions in the peripheral class also haveThisPointer, which points to an object of the peripheral type. This object only has members defined in the peripheral class.

6,Nested classes can directly reference static members, type names, andEnumeration member. To reference A type name or static member outside the peripheral scope, You Need To determine the scope operator.

7 When instantiating a peripheral class template, the nested class in the class template is not automatically instantiated. Like a member function, a nested class is instantiated only when the nested class itself is used when the complete class type is required. In the above example, Queue <int> qi; Instantiate only Queue <int> But not an instance Queueitem <int> , Only Queue <int> In the member functions of the class Head, tail It is instantiated only when it is referenced. Queueitem <int> Class.

8 . Search for names used in nested classes. Search for the common class name line. When processing the class member declaration, any name used must appear before use. When processing the definition, the entire nested class and peripheral class are in the scope.

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.