📜  C++中的本地类

📅  最后修改于: 2021-05-25 21:24:58             🧑  作者: Mango

在函数内部声明的类成为该函数的局部类,在C++中称为Local Class。例如,在下面的程序中,Test是fun()中的一个本地类。

#include
using namespace std;
  
void fun()  
{
      class Test  // local to fun
      {
        /* members of Test class */
      };
}
  
int main()
{
    return 0;
}

以下是有关本地课程的一些有趣事实。
1)本地类类型名称只能在封闭函数。例如,在以下程序中,t和tp的声明在fun()中有效,但在main()中无效。

#include
using namespace std;
  
void fun()  
{       
      // Local class 
      class Test  
      {
        /* ... */      
      };
  
      Test t;  // Fine
      Test *tp;  // Fine
}
  
int main()
{
    Test t;  // Error
    Test *tp;  // Error
    return 0;
}

2)必须仅在类内部定义Local类的所有方法。例如,程序1运行正常,而程序2编译失败。

// PROGRAM 1
#include
using namespace std;
  
void fun()  
{
    class Test  // local to fun
    {
    public:
    
       // Fine as the method is defined inside the local class   
       void method() {
          cout << "Local Class method() called";
       }
    };      
  
    Test t;
    t.method();  
}
  
int main()
{
    fun();
    return 0;
}

输出:

Local Class method() called
// PROGRAM 2
#include
using namespace std;
  
void fun()
{
    class Test  // local to fun
    {
    public:
        void method();
    };
      
    // Error as the method is defined outside the local class
    void Test::method()
    {
        cout << "Local Class method()";
    }
}
  
int main()
{
    return 0;
}

输出:

Compiler Error:
 In function 'void fun()':
 error: a function-definition is not allowed here before '{' token

3) Local类不能包含静态数据成员。它可能包含静态函数。例如,程序1编译失败,但是程序2运行正常。

// PROGRAM 1
#include
using namespace std;
  
void fun()  
{
      class Test  // local to fun
      {
         static int i;
      };
}
  
int main()
{
    return 0;
}
Compiler Error:
 In function 'void fun()':
 error: local class 'class fun()::Test' shall not have static data member 'int fun()::Test::i'
// PROGRAM 2
#include
using namespace std;
  
void fun()
{
    class Test  // local to fun
    {
    public:
        static void method()
        {
            cout << "Local Class method() called";
        }
    };
  
    Test::method();
}
  
int main()
{
    fun();
    return 0;
}

输出:

Local Class method() called

4)本地类的成员方法只能访问封闭函数的静态和枚举变量。封闭函数的非静态变量无法在本地类中访问。例如,程序1可以编译并正常运行。但是,程序2编译失败。

// PROGRAM 1
#include
using namespace std;
  
void fun()  
{
      static int x;
      enum {i = 1, j = 2};
  
      // Local class
      class Test
      {
        public:
          void method() {
              cout << "x = " << x << endl;   // fine as x is static
              cout << "i = " << i << endl;   // fine as i is enum
          }
      };
  
      Test t;
      t.method();
}
  
int main()
{
    fun();
    return 0;
}

输出:

x = 0
i = 1
// PROGRAM 2
#include
using namespace std;
  
void fun()  
{
      int x;
  
      // Local class
      class Test
      {
        public:
          void method() {
              cout << "x = " << x << endl;
          }
      };
  
      Test t;
      t.method();
}
  
int main()
{
    fun();
    return 0;
}

输出:

In member function 'void fun()::Test::method()':
  error: use of 'auto' variable from containing function

5)局部类可以访问全局类型,变量和函数。同样,本地类可以访问具有相同函数的其他本地类。 。例如,以下程序可以正常运行。

#include
using namespace std;
  
int x;
  
void fun()  
{
  
      // First Local class
      class Test1 {
      public:
         Test1() { cout << "Test1::Test1()" << endl; }
      };
  
      // Second Local class
      class Test2
      {
           // Fine: A local class can use other local classes of same function
           Test1 t1;
      public:
          void method() {
              // Fine: Local class member methods can access global variables.
              cout << "x = " << x << endl;
          }
      };
  
      Test2 t;
      t.method();
}
  
int main()
{
    fun();
    return 0;
}

输出:

Test1::Test1()
x = 0

另请参见C++中的嵌套类

参考:
本地类(仅C++)
本地班

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”