C++ 知识点18 内部类

C++ 内部类(嵌套类)


一、什么是内部类

内部类 / 嵌套类 :在一个类的里面再定义另一个类

cpp 复制代码
class Outer     // 外部类
{
    class Inner // 内部类
    {
        
    };
};

关键点:

  1. 内部类是外部类的成员

  2. 内部类受 public / private / protected 访问权限控制

  3. 内部类不默认拥有外部类对象,不能直接随便访问外部类成员


二、基础语法 + 最简示例代码

cpp 复制代码
#include <iostream>
using namespace std;
​
// 外部类
class Outer
{
// 公有内部类:外部可以访问
public:
    class Inner
    {
    public:
        void hello()
        {
            cout << "我是内部类的成员函数" << endl;
        }
    };
};
​
int main()
{
    // 外部类::内部类 定义对象
    Outer::Inner in;
    in.hello();
​
    return 0;
}

输出:

cpp 复制代码
我是内部类的成员函数

写法记住:外部类名::内部类名


三、内部类设为 private(外部无法访问)

cpp 复制代码
#include <iostream>
using namespace std;
​
class Outer
{
// 私有内部类:只能在外部类内部使用
private:
    class Inner
    {
    public:
        void test()
        {
            cout << "私有内部类" << endl;
        }
    };
​
public:
    // 外部类内部可以创建内部类对象
    void createInner()
    {
        Inner in;
        in.test();
    }
};
​
int main()
{
    Outer out;
    out.createInner();
​
    // 错误!Inner 是 private,外部不能访问
    // Outer::Inner in;
​
    return 0;
}

输出:

cpp 复制代码
私有内部类

总结:

  • private 内部类:仅外部类内部可用,外界看不见

  • public 内部类:全局可用 ,通过 Outer::Inner 创建


四、内部类 和 外部类 互相访问规则(重点)

核心规则

  1. 内部类可以访问外部类的静态成员

  2. 内部类不能直接访问外部类普通成员(没有外部类对象)

  3. 要访问外部类普通成员,必须传外部类对象进去

  4. 外部类可以直接使用内部类

代码演示:访问静态成员

cpp 复制代码
#include <iostream>
using namespace std;
​
class Outer
{
public:
    static int num;   // 外部类静态成员
    int age;          // 外部类普通成员
​
    class Inner
    {
    public:
        void show()
        {
            // 可以直接访问外部类静态成员
            cout << "外部类静态num = " << Outer::num << endl;
​
            // 错误:不能直接访问普通成员
            // cout << age << endl;
        }
    };
};
​
// 静态成员初始化
int Outer::num = 100;
​
int main()
{
    Outer::Inner in;
    in.show();
    return 0;
}

代码演示:内部类访问外部类普通成员(传对象)

cpp 复制代码
#include <iostream>
using namespace std;
​
class Outer
{
public:
    int age = 20;
​
    class Inner
    {
    public:
        // 传入外部类对象
        void getOuterAge(const Outer& out)
        {
            cout << "外部类age = " << out.age << endl;
        }
    };
};
​
int main()
{
    Outer out;
    Outer::Inner in;
​
    in.getOuterAge(out);
    return 0;
}
输出:

外部类age = 20

五、外部类使用内部类

cpp 复制代码
#include <iostream>
using namespace std;
​
class Outer
{
public:
    class Inner
    {
    public:
        void hello()
        {
            cout << "内部类hello" << endl;
        }
    };
​
    // 外部类函数中直接用内部类
    void useInner()
    {
        Inner in;
        in.hello();
    }
};
​
int main()
{
    Outer out;
    out.useInner();
    return 0;
}

六、内部类也可以有构造函数、成员函数

cpp 复制代码
#include <iostream>
#include <string>
using namespace std;
​
class Outer
{
public:
    class Inner
    {
    private:
        string msg;
    public:
        // 内部类构造函数
        Inner(string s) : msg(s) {}
        void show()
        {
            cout << msg << endl;
        }
    };
};
​
int main()
{
    Outer::Inner in("内部类带参构造");
    in.show();
    return 0;
}

七、内部类 核心考点总结(必背)

  1. 内部类 = 定义在类里面的类

  2. 访问方式:外部类::内部类

  3. 内部类也有 public/private/protected 权限;

  4. private 内部类只能外部类内部使用,外部无法创建;

  5. 内部类可直接访问外部类静态成员

  6. 内部类访问外部类普通成员,必须传入外部类对象;

  7. 外部类可以直接随便使用内部类

  8. 内部类是独立的类,不隐含 this 指向外部类。

相关推荐
weixin_440784117 小时前
【HandlerThread实现原理】
android·java·开发语言
天空'之城7 小时前
C 语言工业级通用组件手写 24:互补滤波
c语言·开发语言·互补滤波·嵌入式算法·工业级组件
XR1234567888 小时前
工厂车间无线网络方案对比:AGV漫游哪家强?
开发语言·php
傻啦嘿哟8 小时前
某短视频平台视频爬虫实战:抓取推荐流视频信息,绕过反爬的3种技巧
开发语言·爬虫·python
現実君9 小时前
【硬件进阶】AD22上位替换JLEDA教程
开发语言·php
mubei-1239 小时前
SpringDAO的用法
java·开发语言·数据库
小程故事多_809 小时前
从A2C、TRPO、PPO到GRPO,强化学习策略梯度算法完整演进与大模型落地实战解析
人工智能·算法
2601_9561219711 小时前
二分算法(知识点+题目)
c++·算法
格林威11 小时前
多相机微秒级对齐:硬件触发 vs PTP(IEEE 1588)方案实战对比
开发语言·人工智能·数码相机·机器学习·计算机视觉·视觉检测·机器视觉
初级代码游戏13 小时前
iOS开发 Swift 速记2:三种集合类型 Array Set Dictionary
开发语言·ios·swift