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

放肆的年华 提交于 2020-02-17 02:16:29
#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日学习了类成员函数指针的概念,暂时还不懂使用场景,写了一个很僵硬的例子

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!