What is the role of nested structures defined in a class?

Source: Internet
Author: User

          • Class A
          • {
          • struct B;
          • Class C
          • {
          • ..........
          • }
          • b* b;
          • Public:
          • void F();
          • void g();
          • };

The first is the problem of namespaces, class or structure defined in the class is not afraid of duplicate names, such as the template class each class has a iterator nested class, the same name does not matter, the greater advantage is also can use this feature to write template functions:

    • Template<class T, class Func>
    • void foreach(T& container, Func f)
    • {
    • for(T::iterator I = container. Begin(); i ! = Container. End(); + +i)
    • {
    • F(*i);
    • }
    • }

The second is that classes can be defined as private or protected, providing access protection mechanisms.

The third is logically good understanding, code easy to understand some. If a class is called ListItem, it might not be possible to realize that the Item is in the ListBox class, and if it becomes listbox::item, it will be a glance.

Define another class or struct within a class, which we call a nested class (nested Class), or a nested type. The introduction of such a nested class is often due to the fact that the perimeter class needs to use a nested class object as the underlying implementation, and that the nested class is used only for the implementation of the perimeter class, and that the underlying implementation can be hidden from the user at the same time .

Although a nested class is defined inside a perimeter class, it is a separate class that is basically not related to the perimeter class. Its members are not part of the perimeter class, and similarly, members of the perimeter class do not belong to that nested class. The appearance of nested classes only tells the perimeter class that there is one such type member for use by the perimeter class. Also, the perimeter class does not have any privileges for access to nested class members, and so does the nested class's access to the perimeter class members, all of which follow the label access control that the normal class has.

As mentioned earlier, another reason for using nested classes is to achieve the hidden purpose of the underlying implementation. To achieve this, we need to define the nested class in another header file, and only forward the nested class in the perimeter class. Of course, when defining this nested class outside the perimeter class, you should use a perimeter class to qualify. When used, you only need to include this header file in the implementation file of the perimeter class.

In addition, nested classes can refer directly to the static members, type names, and enumeration members of the perimeter class, even if these are private.

  

  

What is the role of nested structures defined in the

class?

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.