• C++ -嵌套类和局部类


     1 #include<iostream>
     2 using namespace std;
     3 class  A{
     4 public:
     5     class B{
     6     public:
     7         B(char *name){cout<<"Constructing B:"<<name<<endl;}
     8     private:
     9         char *name;
    10     };
    11     //B现在是类A空间的一个数据类型
    12     B b;
    13     A():b("In class A"){
    14         cout<<"Constructing A"<<endl;
    15         //cout<<name<<endl;  访问不到
    16     }
    17 };
    18 int main(){
    19     A a;
    20     A::B b("Outside class A");
    21 }
    22 /*
    23 输出:
    24 Constructing B:In class A
    25 Constructing A
    26 Constructing B:Outside class A
    27 
    28 外围类与嵌套类是两个完全独立的类,并没有其他特殊的关系,也就是说
    29 嵌套类的成员和外围类的成员没有任何关系,它们不能够互相访问,也
    30 不存在友元的关系。
    31 
    32 */

    2、局部类

     1 #include<iostream>
     2 using namespace std;
     3 void func(){
     4     static int s;
     5     class A{
     6     public:
     7         int num;
     8         void init(int i){ s = i;}
     9     };
    10     A a;
    11     a.init(8);
    12     cout<<s<<endl;
    13 }
    14 int main(){
    15     func();
    16 }
    17 /*
    18 1、局部类只能在定义它的函数内部使用,其他地方不能使用
    19 2、局部类的所有成员函数都必须定义在类体内。
    20 3、局部类的成员函数,除了可以访问成员函数自己的局部变量,
    21     类自己的成员变量、全局变量、全局静态变量,还可以访问定义
    22     局部类函数的静态变量。
    23 4、局部类中不能定义静态数据成员。
    24 
    25 */
  • 相关阅读:
    笔记本搜不到无线网络连接[转]
    局域网IP冲突
    CFree 修改/添加编译配置环境(Build Configuration)
    字母索引网页
    NAOChoregraphe"单机使用许可证使用了多次"问题解决方案
    redis源码笔记-dict.c
    redis源码笔记-sds
    redis源码笔记-testhelp
    redis源码笔记-endian
    redis源码笔记-dict.h
  • 原文地址:https://www.cnblogs.com/teng-IT/p/6026751.html
Copyright © 2020-2023  润新知