吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 482|回复: 7
收起左侧

[学习记录] MFC动态创建Demo

[复制链接]
lrj2025kernel 发表于 2026-8-14 12:51
本帖最后由 lrj2025kernel 于 2026-8-14 12:56 编辑

[] 纯文本查看 复制代码
#include <iostream>
#include <cstring>

class Object;

// ============================================================
// RuntimeClass
// ============================================================

struct RuntimeClass
{
        const char* name;

        // 父类的 RuntimeClass
        RuntimeClass* base;

        // 动态创建函数
        Object* (*createObject)();

        // 所有 RuntimeClass 组成一个链表
        RuntimeClass* next;

        // 链表头
        static RuntimeClass* first;

        // --------------------------------------------------------
        // 根据 RuntimeClass 创建对象
        // --------------------------------------------------------
        Object* CreateObject()
        {
                if (createObject == nullptr)
                {
                        std::cout << "这个类不支持动态创建\n";
                        return nullptr;
                }

                // 调用函数指针
                return (*createObject)();
        }

        // --------------------------------------------------------
        // 根据类名查找 RuntimeClass
        // --------------------------------------------------------
        static RuntimeClass* Load(const char* className)
        {
                RuntimeClass* current = first;

                while (current != nullptr)
                {
                        if (std::strcmp(current->name, className) == 0)
                        {
                                return current;
                        }

                        current = current->next;
                }

                return nullptr;
        }
};


// RuntimeClass 的静态成员必须在类外定义
RuntimeClass* RuntimeClass::first = nullptr;


// ============================================================
// Object
// ============================================================

class Object
{
public:

        virtual ~Object()
        {
        }

        // 返回当前对象真正的 RuntimeClass
        virtual RuntimeClass* GetRuntimeClass() const = 0;

        // --------------------------------------------------------
        // 判断:当前对象是不是 target 类型
        // --------------------------------------------------------
        bool IsKindOf(RuntimeClass* target)
        {
                RuntimeClass* current = GetRuntimeClass();

                while (current != nullptr)
                {
                        if (current == target)
                        {
                                return true;
                        }

                        // 沿着继承关系向父类走
                        current = current->base;
                }

                return false;
        }

        virtual void SayHello()
        {
                std::cout << "Hello Object\n";
        }
};


// ============================================================
// View
// ============================================================

class View : public Object
{
public:

        static RuntimeClass runtimeClass;

        RuntimeClass* GetRuntimeClass() const override
        {
                return &runtimeClass;
        }

        void SayHello() override
        {
                std::cout << "Hello View\n";
        }
};


// View 的 RuntimeClass
RuntimeClass View::runtimeClass =
{
        "View",

        nullptr,       // View 没有我们模拟的父类

        nullptr,       // View 不支持动态创建

        nullptr
};


// ============================================================
// MyView
// ============================================================

class MyView : public View
{
public:

        static RuntimeClass runtimeClass;

        // --------------------------------------------------------
        // 动态创建函数
        // --------------------------------------------------------
        static Object* CreateObject()
        {
                return new MyView;
        }

        RuntimeClass* GetRuntimeClass() const override
        {
                return &runtimeClass;
        }

        void SayHello() override
        {
                std::cout << "Hello MyView\n";
        }
};


// MyView 的 RuntimeClass
RuntimeClass MyView::runtimeClass =
{
        "MyView",

        &View::runtimeClass,

        &MyView::CreateObject,

        nullptr
};


// ============================================================
// 注册 RuntimeClass
// ============================================================

// 把 RuntimeClass 加入链表
void RegisterRuntimeClass(RuntimeClass* runtimeClass)
{
        runtimeClass->next = RuntimeClass::first;
        RuntimeClass::first = runtimeClass;
}


// ============================================================
// main
// ============================================================

int main()
{
        // --------------------------------------------------------
        // 1. 注册类
        // --------------------------------------------------------

        RegisterRuntimeClass(&View::runtimeClass);
        RegisterRuntimeClass(&MyView::runtimeClass);


        // --------------------------------------------------------
        // 2. 测试 IsKindOf
        // --------------------------------------------------------

        MyView* p = new MyView;

        std::cout << std::boolalpha;

        std::cout
                << "MyView 是 MyView 吗:"
                << p->IsKindOf(&MyView::runtimeClass)
                << '\n';

        std::cout
                << "MyView 是 View 吗:"
                << p->IsKindOf(&View::runtimeClass)
                << '\n';


        // --------------------------------------------------------
        // 3. 根据类名查找 RuntimeClass
        // --------------------------------------------------------

        const char* className = "MyView";

        RuntimeClass* pClass =
                RuntimeClass::Load(className);


        if (pClass == nullptr)
        {
                std::cout << "找不到类:"
                        << className
                        << '\n';

                delete p;
                return 0;
        }


        // --------------------------------------------------------
        // 4. 动态创建对象
        // --------------------------------------------------------

        Object* pObj =
                pClass->CreateObject();


        if (pObj != nullptr)
        {
                pObj->SayHello();
        }


        // --------------------------------------------------------
        // 5. 释放对象
        // --------------------------------------------------------

        delete p;
        delete pObj;
        getchar();
        return 0;
}

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

fjqisba 发表于 2026-8-14 14:53
这个年代了还在学MFC吗,没饭吃
 楼主| lrj2025kernel 发表于 2026-8-14 15:00
bigcount 发表于 2026-8-14 15:35
MFC不是搞界面吗?这个看起来像是命令提示符窗口
 楼主| lrj2025kernel 发表于 2026-8-14 15:38
bigcount 发表于 2026-8-14 15:35
MFC不是搞界面吗?这个看起来像是命令提示符窗口

这里模拟的是MF框架C的一些核心机制,MFC不只是界面
XIUDAO 发表于 2026-8-14 16:13
emmmm.....
Rodriguezs 发表于 2026-8-15 01:07
fjqisba 发表于 2026-8-14 14:53
这个年代了还在学MFC吗,没饭吃

很多安全代码都只有MFC版本。
Rodriguezs 发表于 2026-8-15 01:09
fjqisba 发表于 2026-8-14 14:53
这个年代了还在学MFC吗,没饭吃

学什么和用什么无关,练散打时也练空拳,上擂台时不用,但不代表不需要练。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - 52pojie.cn ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2026-8-25 18:38

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表