📜  C++ 中的本地类

📅  最后修改于: 2022-05-13 01:55:34.713000             🧑  作者: Mango

C++ 中的本地类

在函数内部声明的类成为该函数的本地类,在 C++ 中称为本地类。

  • 本地类名只能在本地使用,即在函数内部而不是在函数外部。
  • 本地类的方法只能在其中定义。
  • 本地类可以具有静态函数,但不能具有静态数据成员。

例如,在下面的程序中,Test 是 fun() 中的一个本地类。

CPP
// C++ program without any compilation error
// to demonstrate a Local Class
#include 
using namespace std;
  
// Creating the class
void fun()
{
    // local to fun
    class Test {
        // members of Test class
    };
}
  
// Driver Code
int main() { return 0; }


CPP
// A program without any compilation error to demonstrate
// that a local class type name can only be used
// in the enclosing function
  
#include 
using namespace std;
  
void fun()
{
    // Local class
    class Test {
        // Body
    };
  
    Test t; // Fine
    Test* tp; // Fine
}
  
int main()
{
    Test t; // Error
    Test* tp; // Error
    return 0;
}


CPP
// C++ program without any compilation error to demonstrate
// that all the methods of Local classes must be defined
// inside the class only
#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;
}


CPP
// C++ program with compilation error to demonstrate that
// all the methods of Local classes must be defined inside
// the class only
#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; }


CPP
// A program with compilation error to demonstrate that
// a Local class cannot contain static data members
#include 
using namespace std;
  
void fun()
{
    class Test // local to fun
    {
        static int i;
    };
}
  
int main() { return 0; }


CPP
// C++ program without any compilation error to demonstrate
// that a Local class cannot contain static data members
#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;
}


CPP
// C++ program without any compilation error to demonstrate
// that member methods of local class can only access static
// and enum variables of the enclosing function
#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;
}


CPP
// C++ program with compilation error to demonstrate that
// member methods of local class can only access static
// and enum variables of the enclosing function
#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;
}


CPP
// C++ program without any compilation error to demonstrate
// that Local classes can access global types, variables and
// functions
#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;
}


以下是关于 C++ 中本地类的一些有趣事实:

1)本地类类型名称只能在封闭函数中使用。

例如,在下面的程序中,t 和 tp 的声明在 fun() 中有效,但在 main() 中无效。

CPP

// A program without any compilation error to demonstrate
// that a local class type name can only be used
// in the enclosing function
  
#include 
using namespace std;
  
void fun()
{
    // Local class
    class Test {
        // Body
    };
  
    Test t; // Fine
    Test* tp; // Fine
}
  
int main()
{
    Test t; // Error
    Test* tp; // Error
    return 0;
}

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

方案一:

CPP

// C++ program without any compilation error to demonstrate
// that all the methods of Local classes must be defined
// inside the class only
#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

方案二:

CPP

// C++ program with compilation error to demonstrate that
// all the methods of Local classes must be defined inside
// the class only
#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 工作正常。

方案一:

CPP

// A program with compilation error to demonstrate that
// a Local class cannot contain static data members
#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'

方案二:

CPP

// C++ program without any compilation error to demonstrate
// that a Local class cannot contain static data members
#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 编译失败。

方案一:

CPP

// C++ program without any compilation error to demonstrate
// that member methods of local class can only access static
// and enum variables of the enclosing function
#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

方案二:

CPP

// C++ program with compilation error to demonstrate that
// member methods of local class can only access static
// and enum variables of the enclosing function
#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;
}

错误:

5)本地类可以访问全局类型、变量和函数。此外,本地类可以访问相同函数的其他本地类。 例如,以下程序可以正常工作。

CPP

// C++ program without any compilation error to demonstrate
// that Local classes can access global types, variables and
// functions
#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++ 中的嵌套类