C + + nested classes (inner and outer classes)

Source: Internet
Author: User

A class defined in a class is called a nested class, and a class that defines a nested class is called an external class.nesting of classes is often done to help implement another class and avoid name collisions. Nesting of classes is often done to help implement another class and avoid name collisions. As follows:
  
 
  1. class queue
  2. {
  3. private:
  4. struct Node{ Item item;struct Node *next };
  5. ...
  6. };
Because structs are members of the public by default,so node is actually a nested class.
    • Scope
The declaration position of a nested class determines the scope of the nested class, that is, it determines the part of the program that can create objects of nested classes. If a nested class is declared in the private part of a class, only the outer class of the nested class can know it. This is the case for the class above. In the queue class, a queue member can use a pointer to a node object or a Node object, but other parts of the program do not even know that the node class exists. For classes derived from the queue, there is no known node. If a nested class is declared in the protected section of a class, it is visible to the outside world. The derived class knows the nested class and can create objects of this type directly. If a nested class is declared in the public part of a class, the latter, the derived class of the latter, and the external world are allowed to use it. Then, when used externally, you must add an outer class scope restriction to the outer class.
 
   
  
  1. class team
  2. {
  3. public
  4. struct Coach{...};
  5. ...
  6. };
When using coaches, the Team::coach teach1 should be defined as such;
Nested structures and enumerations are scoped to this same. Many programmers use public enumerations to provide class constants that are used by customers.
The following table summarizes the scope characteristics of nested classes, structs, and enumerations.
Declaration Location Whether the class containing it can use it Whether a class derived from the class that contains it can use it Whether it can be used externally
Public Is Is Is
Private Is Whether Whether
Protection Is Is Whether
    • Access rights
Declaring a nested class in an external class does not give the external class any access to the nested class, nor does it give any nested classes access to the external class. Same as general class access control (private, public, protected).

    • Code testing
Accessing nested classes in an external class
  
 
  1. class test
  2. {
  3. public:
  4. test()
  5. {
  6. i = 10; //不能访问
  7. mytest::i = 10;//不能访问
  8. }
  9. private:
  10. class mytest
  11. {
  12. int i;
  13. int j;
  14. };
  15. };
You cannot directly access the members of MyTest and try to access them through objects.
  
 
  1. class test
  2. {
  3. public:
  4. test()
  5. {
  6. cc.i = 10; //通过对象可以访问,如果i为私有则不可访问
  7. }
  8. private:
  9. class mytest
  10. {
  11. public:
  12. int i;
  13. int j;
  14. };
  15. mytest cc;
  16. };
By testing, you declare an object of a nested class in an external class. The object is then used in the outer class to access the nested class, with the same rules as the normal class.
The external class is accessed in a nested class because there is no access to the outer class in the nested class. Therefore, only the modified object is defined in the nested class to be able to access its non-static members, but at this point the outer class is an incomplete type (the class does not have a definition complete). Therefore, it is not possible to access external classes within nested classes.


From for notes (Wiz)

C + + nested classes (inner and outer classes)

Related Article

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.