zoukankan      html  css  js  c++  java
  • C++嵌套类

    C++支持类的嵌套,其语法比较简单。代码是最好的说明。

     1 #include <iostream>
     2 using namespace std;
     3 
     4 class Outter{
     5     public:
     6         int a;
     7         static int s;
     8         //nested class and enclosing class are independent.
     9         class Inner{ 
    10             public:
    11             int b;
    12             //nested class can access static members of enclosing class
    13             int foo(){ return b + s;}
    14             int bar();
    15         };
    16 };
    17 
    18 int Outter::s = 1;
    19 //define a nested class member outside of the class declaration.
    20 int Outter::Inner::bar(){
    21     return 0xdeadbeef;
    22 }
    23 
    24 int main(){
    25     Outter o;
    26     o.a = 0;
    27     //if nested class is specified as public, then everyone can use the nested class.
    28     //if nested class is specified as protected, then only the enclosing class,
    29     //friend of the enclosing class and derived class of the enclosing class can access the nested class.
    30     //if the nested class is specified as private, then only the enclosing class
    31     //and friend of the enclosing class can access the nested class.
    32     Outter::Inner i;
    33     i.b = 1;
    34     cout<<i.foo()<<endl;
    35     cout<<hex<<i.bar()<<endl;
    36     return 0;
    37 }
  • 相关阅读:
    二维树状数组(模板)
    3033太鼓达人
    2503相框
    Ant Trip(画几笔)
    [ZJOI2004]嗅探器
    [USACO06JAN]冗余路径Redundant Paths(缩点)
    P3806 【模板】点分治1
    P4149 [IOI2011]Race
    P2634 [国家集训队]聪聪可可
    P4178 Tree
  • 原文地址:https://www.cnblogs.com/richardustc/p/3045166.html
Copyright © 2011-2022 走看看