C++学习笔记(24)

255. 如何取代虚函数

虚函数在执行时会跳转两次:一次查找对象的虚函数表(vtable),再通过表中的地址找到实际的函数地址。这会导致 CPU 每次跳转时预取指令失效,降低效率。因此,使用 std::function 代替虚函数,通过绑定子类的成员函数,实现类似虚函数的功能。代码如下:

复制代码
#include <iostream>
#include <functional>
using namespace std;

struct Hero {  // 英雄基类
    function<void()> m_callback;  // 用于绑定子类的成员函数

    // 注册子类成员函数,子类成员函数没有参数
    template<typename Fn, typename ...Args>
    void callback(Fn&& fn, Args&&... args) {
        m_callback = bind(forward<Fn>(fn), forward<Args>(args)...);
    }

    void show() { m_callback(); }  // 调用子类的成员函数
};

struct XS : public Hero {  // 西施派生类
    void show() { cout << "西施释放了技能。\n"; }
};

struct HX : public Hero {  // 韩信派生类
    void show() { cout << "韩信释放了技能。\n"; }
};

int main() {
    int id = 0;  // 英雄的 id
    cout << "请输入英雄(1-西施;2-韩信):";
    cin >> id;

    Hero* ptr = nullptr;
    if (id == 1) {
        ptr = new XS;
        ptr->callback(&XS::show, static_cast<XS*>(ptr));  // 注册子类成员函数
    }
    else if (id == 2) {
        ptr = new HX;
        ptr->callback(&HX::show, static_cast<HX*>(ptr));  // 注册子类成员函数
    }

    if (ptr != nullptr) {
        ptr->show();  // 调用子类的成员函数
        delete ptr;  // 释放派生类对象
    }
}

299. 课后作业

一、选出妃子、宫女和嬷嬷
  1. 妃子:年龄 18-25 岁,身高 165-178cm,身材火辣,颜值漂亮。
  2. 宫女:年龄 18-30 岁,身高 160-165cm,身材火辣或普通,颜值漂亮。
  3. 嬷嬷:年龄 35-40 岁,身高 155-165cm,身材普通或飞机场,颜值一般。
复制代码
#include <iostream>
#include <cstring>
using namespace std;

struct st_girl {  // 超女结构体
    int age;
    int height;
    char sc[31];  // 身材
    bool yz;      // true-漂亮,false-一般
} stgirl;

int main() {
    // 先输入超女全部的数据项
    cout << "请输入年龄:"; cin >> stgirl.age;
    cout << "请输入身高:"; cin >> stgirl.height;
    cout << "请输入身材:"; cin >> stgirl.sc;
    cout << "请输入颜值(1-漂亮,0-一般):"; cin >> stgirl.yz;

    cout << "age=" << stgirl.age << ", height=" << stgirl.height << ", sc=" << stgirl.sc << ", yz=" << stgirl.yz << endl;

    // 判断妃子
    if (stgirl.age >= 18 && stgirl.age <= 25 && stgirl.height >= 165 && stgirl.height <= 178 && strcmp(stgirl.sc, "火辣") == 0 && stgirl.yz) {
        cout << "妃子\n";
    }
    // 判断宫女
    else if (stgirl.age >= 18 && stgirl.age <= 30 && stgirl.height >= 160 && stgirl.height <= 165 && (strcmp(stgirl.sc, "火辣") == 0 || strcmp(stgirl.sc, "普通") == 0) && stgirl.yz) {
        cout << "宫女\n";
    }
    // 判断嬷嬷
    else if (stgirl.age >= 35 && stgirl.age <= 40 && stgirl.height >= 155 && stgirl.height <= 165 && (strcmp(stgirl.sc, "普通") == 0 || strcmp(stgirl.sc, "飞机场") == 0) && !stgirl.yz) {
        cout << "嬷嬷\n";
    }
}
二、根据数字判断月份
  1. 使用 if-else if 语句实现。
  2. 使用 switch 语句实现。
  3. 使用字符串数组实现。
复制代码
#include <iostream>
using namespace std;

int main() {
    int month;
    cout << "请输入月份:"; 
    cin >> month;
    cout << "month = " << month << endl;

    // 使用字符串数组实现
    string montharr[12] = {
        "January", "February", "March", "April", "May", "June", 
        "July", "August", "September", "October", "November", "December"
    };

    if (month >= 1 && month <= 12)
        cout << montharr[month - 1] << endl;
    else
        cout << "输入的数字不正确。\n";
}
相关推荐
程序喵大人29 分钟前
【C++进阶】STL算法与函数对象 - 09 函数对象保存状态并复用规则
开发语言·c++·算法·stl·函数对象
ShineWinsu6 小时前
对于C++:auto_ptr、unique_ptr、shared_ptr的模拟实现
c++·面试·笔试·智能指针·unique_ptr·shared_ptr·auto_ptr
hsjiasb7 小时前
FreeRTOS学习(二十七)——任务栈与栈溢出检测
开发语言·stm32·学习·学习笔记·freertos
小弥儿7 小时前
GitHub今日热榜 | 2026-08-16:大模型微调与编码工具扎堆
学习·开源·github
2501_926978338 小时前
AGI 的四种瓶颈:资源型还是发现型--以及DSH的位置
人工智能·经验分享·笔记·ai写作
XLYcmy8 小时前
京东 算法实习一面 下+手撕
c++·python·llm·概率论·数据处理·训练·codebert
反转180度9 小时前
Qt 6 + C++17 实现 SFTP 批量部署:工业设备运维工具实战解析
运维·c++·qt
liuqs3329 小时前
nat123安卓和nat123全端口选型指南,映射工具怎么挑
网络·笔记
xian_wwq9 小时前
【学习笔记】光伏“四可”装置纵密模块--配置方法和原理
笔记·学习
min(a,b)10 小时前
学习第17天:Agent Memory 与 MCP 协议
python·学习