用了很久,也私有了很久~好像以前也想到过~
mLoginBtn->subscribeEvent( CEGUI::Window::EventMouseClick, CEGUI::Event::Subscriber( &GuiLogin::handleClickBtnLogin, this ) );
handleClickBtnLogin是私有的函数,Subscriber将函数指针的地址和this对象指针保存起来了。。
为啥不报访问私有函数的错误那?
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//privateMemberFuncPtr.h
#pragma once
#include "stdio.h"
class SlotFunctorBase
{
public:
virtual ~SlotFunctorBase() {};
virtual void operator()() = 0;
};
SlotFunctorBase* g_SlotFunctorBase;
template<typename T>
class MemberFunctionSlot : public SlotFunctorBase
{
public:
//! Member function slot type.
typedef void(T::*MemberFunctionType)();
MemberFunctionSlot(MemberFunctionType func, T* obj) :
d_function(func),
d_object(obj)
{}
virtual void operator()()
{
return (d_object->*d_function)();
}
private:
MemberFunctionType d_function;
T* d_object;
};
class PrivateMemberFuncPtr
{
public:
void bind();
private:
void print()
{
printf("success/n");
}
};
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//privateMemberFuncPtr.cpp
#include "PrivateMemberFuncPtr.h"
void PrivateMemberFuncPtr::bind()
{
g_SlotFunctorBase = new MemberFunctionSlot<PrivateMemberFuncPtr>(&PrivateMemberFuncPtr::print, this);
}
void main()
{
PrivateMemberFuncPtr* privateMemberFuncPtr = new PrivateMemberFuncPtr;
privateMemberFuncPtr->bind();
g_SlotFunctorBase->operator ()();
}
1.main中生成
privateMemberFuncPtr 0x003d3518
2.void PrivateMemberFuncPtr::bind()函数中
0x00411580 PrivateMemberFuncPtr::print(void)
3.跟到MemberFunctionSlot的构造函数中
MemberFunctionSlot(MemberFunctionType func, T* obj) :
d_function(func),
d_object(obj)
func = 0x00411131 PrivateMemberFuncPtr::print(void)
obj = 0x003d3518
为什么会有差异那?估计在
virtual void MemberFunctionSlot::operator()()
{
return (d_object->*d_function)();
}
中可以间接访问私有函数的关键就在上面黄色函数地址发生了变化~希望高手能解答下~