C++局部类

来源:互联网 发布:黄金投资 知乎 编辑:程序博客网 时间:2024/06/06 04:34

1.局部类的定义

在一个函数中声明的类称为局部类。例如,下面程序中,Test就是fun函数中的一个局部类。
#include<iostream>using namespace std;void fun() {  class Test  // 局部类  {    //其它成员  };}int main(){    return 0;}

2.局部类的有用事项

2.1.局部类作用域

局部类的类名称只能在其所处的函数中使用。 例如下面程序中,t和tp的声明在fun函数中是有效的,但main函数中无效。

#include<iostream>using namespace std; void fun() {            // 局部类      class Test       {        //类成员...           };       Test t;    // OK      Test *tp;  // OK} int main(){    Test t;    // Error    Test *tp;  // Error    return 0;}

2.2.局部类成员方法的定义

局部类的所有成员方法只能定义在类的内部。
例如,下面的例子1工作正常,而例子2 编译失败。

// 例子1#include<iostream>using namespace std; void fun() {    class Test  // fun函数中的局部类    {    public:          // method定义在类的内部,ok        void method() {          cout << "Local Class method() called";       }    };          Test t;    t.method(); } int main(){    fun();    return 0;}
输出:
Local Class method() called
// 例子2#include<iostream>using namespace std; void fun(){    class Test  // fun函数中的局部类    {    public:        void method();    };         // 错误。method定义在了局部类的外面    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

2.3.局部类与静态类型

局部类不能包含静态数据成员。但可以包含静态函数。
例如下面的例子1编译失败,但例子2工作正常。

// 例子1#include<iostream>using namespace std; void fun() {      class Test  //局部类包含静态成员      {         static int i;      };} int main(){    return 0;}
编译错误:
 In function 'void fun()': error: local class 'class fun()::Test' shall not have static data member 'int fun()::Test::i'
// 例子2#include<iostream>using namespace std; void fun(){    class Test  //局部类包含静态成员方法    {    public:        static void method()        {            cout << "Local Class method() called";        }    };     Test::method();} int main(){    fun();    return 0;}
编译正常,输出:
Local Class method() called

2.4.局部类成员方法在函数中的访问权限

局部类的成员函数只能访问函数中的静态变量以及枚举变量。无法访问非静态变量。
例如,例子1运行正常,但是例子2编译失败。

// 例子1#include<iostream>using namespace std; void fun() {      static int x;      enum {i = 1, j = 2};       // Local class      class Test      {        public:          void method() {              cout << "x = " << x << endl;   // x是静态变量,可以访问              cout << "i = " << i << endl;   // i是枚举,可以访问          }      };       Test t;      t.method();} int main(){    fun();    return 0;}
输出:
x = 0
i = 1

// 例子2#include<iostream>using namespace std; void fun() {      int x;       // 局部类      class Test      {        public:          void method() {              cout << "x = " << x << endl; //无法访问非静态变量x          }      };       Test t;      t.method();} int main(){    fun();    return 0;}

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

2.5.局部类与全局数据

局部类可以访问全局类型,变量和函数。另外,局部类可以访问同一个函数中的其它局部类。
例如,下面程序工作正常。

#include<iostream>using namespace std; int x; void fun() {       // 第一个局部类      class Test1 {      public:         Test1() { cout << "Test1::Test1()" << endl; }      };       // 第二个局部类      class Test2      {           // 局部类可以使用同一个函数中的其它局部类。           Test1 t1;      public:          void method() {              // 局部类可以访问全局变量              cout << "x = " << x << endl;          }      };       Test2 t;      t.method();} int main(){    fun();    return 0;}
输出:
Test1::Test1()
x = 0

同时,可以参考本系列的另一篇文章"C++嵌套类"。
0 0
原创粉丝点击