框架使用类:静态函数、类名+对象句柄

静态函数 操作非静态函数和非静态变量
对象句柄 操作对象成员
#include <iostream>
#include <Vector>
#include <Map>
#include "WinObjectHandle.h"

using namespace std;
/*静态函数操作 非静态函数或非静态成员变量的方法*/

class A;
typedef map<objHandle, A*> CAMap;

class A
{
public:
    A()
    {
        if( m_map.empty()){
            m_handleId = 1;
        } else {
            m_handleId = m_map.size() +1;
        }
        m_map.insert(CAMap::value_type(m_handleId, this));
    }
    objHandle getHandle()
    {
        cout << "getHandle: " << m_handleId << endl;
        return m_handleId;
    }

    static A* CBGetInstall(objHandle handle)
    {
        if(m_map.empty())
            return NULL;
        CAMap::iterator it = m_map.find(handle);
        if(it == m_map.end())
        {
            cout << "obj no find" << endl;
            return NULL;
        }
        return (*it).second;
    }

    static void CBFunc1(objHandle handle)
    {


        if(m_map.empty()) return;


        CAMap::iterator it = m_map.find(handle);
        if(it != m_map.end())
        {
            A* pThis = it->second;
            pThis->func1();
        }
    }

    void func1()
    {
        cout << m_handleId << "::call func1()" <<endl;
    }

private:
    objHandle m_handleId;
    static CAMap m_map;
};

CAMap A::m_map;


int main()
{
    cout << "Hello world!" << endl;
    objHandle  a1;
    objHandle  a2;

    A *app1 = new A();
    a1 = app1->getHandle();

    A *app2 = new A();
    a2 = app2->getHandle();

    A::CBFunc1(a1);

    cout << "<<<<<<<<<<<<<<<<! " <<endl;
    /*回调函数,不建议增加返回整个对象的回调函数,开放的权限过大。*/
    A::CBGetInstall(a1)->func1();
    cout << "<<<<<<<<<<<<<<<<! " <<endl;
    A::CBGetInstall(a2)->CBFunc1(a1); /*不建议这样使用*/
    return 0;
}




发布了41 篇原创文章 · 获赞 13 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/wade_510/article/details/72617127