【笔记】模板类成员函数指针的使用

#include <stdio.h>
#include <stdlib.h>
#include <map>

#define TRUE 1
#define FALSE 0

/// 单向链表队列模板类
template<typename T>
class TMyQueue
{
public:
    TMyQueue()
    {
        head_ = NULL;
        tail_ = NULL;
        max_size_ = 10;
        size_ = 0;
    }
    TMyQueue(int max_size)
    {
        head_ = NULL;
        tail_ = NULL;
        max_size_ = max_size;
        size_ = 0;
    }
    ~TMyQueue()
    {
        while (head_ != NULL)
        {
            Node *tmp = head_;
            head_ = head_->next;
            free(tmp);
        }
        tail_ = NULL;
        printf("[~TMyQueue]head_ = %p, tail_ = %p\n", head_, tail_);
    }

    bool IsFull()
    {
        return (size_ == max_size_);
    }
    bool IsEmpty()
    {
        return (size_ == 0);
    }
    int QueueSize()
    {
        return size_;
    }
    bool Push(T &item)
    {
        if (IsFull())
        {
            printf("push error\n");
            return FALSE;
        }

        Node *tmp = new Node();
        tmp->item = item;
        tmp->next = NULL;
        size_++;

        if (head_ == NULL)
        {
            head_ = tmp;
            tail_ = tmp;
        }
        else
        {
            tail_->next = tmp;
            tail_ = tmp;
        }

        printf("queue have %02d num, head_ = %p, tail_ = %p, push ok at address %p\n", size_, head_, tail_, tmp);

        return TRUE;
    }
    bool Pop(T &item)
    {
        if (IsEmpty())
        {
            printf("pop error\n");
            return FALSE;
        }

        Node *tmp = head_;
        head_ = head_->next;
        item = tmp->item;
        size_--;
        free(tmp);
        if (head_ == NULL)
            tail_ = NULL;
        printf("queue have %02d num, head_ = %p, tail_ = %p, pop ok at address %p\n", size_, head_, tail_, tmp);

        return TRUE;
    }
private:
    typedef struct Node
    {
        T item;
        Node *next;
    }Node;

private:
    Node *head_;
    Node *tail_;
    int max_size_;
    int size_;
};

class TMyHandle
{
public:
    enum
    {
        PUSH = 0,
        POP = 1
    };
public:
    /// 定义PFUNC类型为一个指向模板类TMyQueue<int>成员函数的指针,返回值为bool,参数为int&
    typedef bool (TMyQueue<int>::*PFUNC)(int &a);
public:
    TMyHandle()
    {
        printf("constructor TMyHandle\n");
    }
    ~TMyHandle()
    {
        printf("destructor TMyHandle\n");
    }

    /// cmd为map的索引,func为注册的模板类TMyQueue<int>成员函数指针
    bool RegisterHandle(int cmd, PFUNC func)
    {
        std::map<int, PFUNC>::iterator it = pFunc_.find(cmd);
        if (it != pFunc_.end())
            return FALSE;

        pFunc_[cmd] = func;
        return TRUE;
    }
    /// obj为模板类TMyQueue<int>的对象指针,pFunc_[cmd]为该对象的成员函数指针
    bool OnCall(TMyQueue<int> *obj, int cmd, int &a)
    {
        std::map<int, PFUNC>::iterator it = pFunc_.find(cmd);
        if (it == pFunc_.end())
            return FALSE;

        /// !!!!!!!!!!!!!!!!!!!!调用很重要
        (obj->*pFunc_[cmd])(a);
        return TRUE;
    }
private:
    /// pFunc_为PFUNC类型(模板类TMyQueue<int>成员函数指针)的map对象
    std::map<int, PFUNC> pFunc_;
};

int main(int argc, char *argv[])
{

    printf("input 0 : PUSH\n");
    printf("input 1 : POP\n");
    int option = -1;
    scanf("%d", &option);

    ///创建模板类TMyQueue<int>对象指针obj
    TMyQueue<int> *obj = new TMyQueue<int>();
    ///创建处理类TMyHandle对象指针handle
    TMyHandle *handle = new TMyHandle();

    switch (option)
    {
        case TMyHandle::PUSH:
        {
            /// 注册模板类TMyQueue<int>成员函数Push的指针
            handle->RegisterHandle(TMyHandle::PUSH, &TMyQueue<int>::Push);

            int value = 0;
            /// 调用注册的处理函数
            handle->OnCall(obj, TMyHandle::PUSH, value);
            printf("PUSH value = %d\n", value);
        }
        break;
        case TMyHandle::POP:
        {
            /// 注册模板类TMyQueue<int>成员函数Ppo的指针
            handle->RegisterHandle(TMyHandle::POP, &TMyQueue<int>::Pop);

            int value = 0;
            /// 调用注册的处理函数
            handle->OnCall(obj, TMyHandle::POP, value);
            printf("POP value = %d\n", value);
        }
        break;
        default:
            printf("input error\n");
        break;
    }

    delete obj;
    delete handle;

    return 0;
}

2020年02月16日学习了类成员函数指针的概念,暂时还不懂使用场景,写了一个很僵硬的例子

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值