zoukankan      html  css  js  c++  java
  • 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 */
  • 相关阅读:
    原型prototype
    this
    作用域、闭包、模块
    嵌入式面试资料
    一些嵌入式面试题目的集锦
    优先级反转
    struct和union的区别
    (转)typedef和#define的用法与区别
    const 和 #define区别
    白话经典算法系列之 快速排序 快速搞定
  • 原文地址:https://www.cnblogs.com/teng-IT/p/6026751.html
Copyright © 2011-2022 走看看