进程管理对象器的实现,数据结构为链表

/**************************************************
WinMain.cpp
进程管理对象器的实现,数据结构为链表
**************************************************/

// Include files
#include <windows.h>
#include <stdio.h>

class cProcessManager
{
  //A structure that stores a function pointer and linked list
 //一个结构体,可以储存一个函数指针和链表的模式

  typedef struct sProcess {
    void  (*Function)();
    sProcess *Next;
  } sProcess;

  protected:
    sProcess *m_ProcessParent; // The top state in the stack 定义一个成员函数
                               // (the head of the stack)

  public:
   cProcessManager() { m_ProcessParent = NULL; } //析构函数 将该结构体指针指向空

    ~cProcessManager()  //析构函数,释放所有链表中的对象
    {
      sProcess *ProcessPtr;

      // Remove all processes from the stack
      while((ProcessPtr = m_ProcessParent) != NULL) {
        m_ProcessParent = ProcessPtr->Next;
        delete ProcessPtr;
      }
    }
 
    // Add function on to the stack 入栈
    void Add(void (*Process)())
    {
      // Don't push a NULL value
  if(Process != NULL) {   //此处插入断点时,调试指向Func1 ,将该结构体块中的Function指向它
        // Allocate a new process and push it on stack
        sProcess *ProcessPtr = new sProcess;
        ProcessPtr->Next = m_ProcessParent;
        m_ProcessParent = ProcessPtr;
        ProcessPtr->Function = Process;
      }
    }

    // Process all functions
    void Process()
    {
        sProcess *ProcessPtr = m_ProcessParent;
        while(ProcessPtr != NULL) {
        ProcessPtr->Function();
        ProcessPtr = ProcessPtr->Next;
      }
    }
};

cProcessManager g_ProcessManager;

// Function prototypes
int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev,          /
                   LPSTR szCmdLine, int nCmdShow);

// Macro to ease the use of MessageBox function
#define MB(s) MessageBox(NULL, s, s, MB_OK);

// Processfunction prototypes - must follow this prototype!
void Func1() { MB("1"); }
void Func2() { MB("2"); }
void Func3() { MB("3"); }

int PASCAL WinMain(HINSTANCE hInst, HINSTANCE hPrev,          /
                   LPSTR szCmdLine, int nCmdShow)
{
  g_ProcessManager.Add(Func1);
  g_ProcessManager.Add(Func2);
  g_ProcessManager.Add(Func3);
  g_ProcessManager.Process();
  //g_ProcessManager.Process();

  return 0;
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值