Introduction and use examples of C++ inner classes

  • 2020-06-12 10:05:54
  • OfStack

introduce

1. Define a class inside another class, and call the class inside an inner class.

Such as:


class A
{
public:
 class B
 {
 public:
 int x;
 int y;
 };
};

Class B is the inner class.

2. The inner class and the outer class have no privileges with each other, that is, the outer class cannot freely access the inner class, and the inner class cannot freely access the outer class.

They are not friends They are not father and son

Use of inner classes


#include <stdio.h>
class A
{
public:
 class B
 {
 public:
 void test()
 {
  printf("this is inner class\n");
 }
 };
};
int main()
{
 A::B b; // Use range symbol ::
 b.test();
}

conclusion


Related articles: