在C++的类中如何实现这样的效果?
typedef void (*EVENT)();
class BaseClass
{
public:
EVENT ev1;
EVENT ev2;
}
class Clent1 : public BaseClass
{
}
class Clent2 : public BaseClass
{
public:
Clent1 obj1;
void funcation(){ printf("welcome"); }
obj1->ev1 = &funcation;
}
意思就是用一个类的派生类的成员函数为另外一个派生类的成员函数指针赋值,这样可以实现事件驱动,请各位给出解答谢谢,如果有别的办法也请告知,谢谢。
问题点数:20、回复次数:15Top
1 楼Wolf0403(废人:独活十年~心如刀割)回复于 2005-01-28 11:37:26 得分 0
参考 boost::signal 和 libsig++Top
2 楼QXK2001(Q龙)回复于 2005-01-28 11:42:32 得分 0
能否给出具体的出处或者代码?Top
3 楼Darkay_Lee()回复于 2005-01-28 12:02:00 得分 0
C++的成员函数指针和JAVA的不一样。
建议你首先搞明白成员函数指针的用法(我大致提示一些,详细的自己google找):
class CCC
{
void func();
};
typedef void (CCC::*MF)();
void main()
{
CCC ccc;
MF mf = &CCC::func;
(ccc.*mf)(); //这里是关键,调用成员函数的指针是需要通过对象来调用的,而不是JAVA那样可以直接记录某个对象的函数指针的。
}
明白了成员函数指针的用法之后修改一下你的设计(我想,大概你需要多一个变量保存对象)。
Top
4 楼xiyi168(风云)回复于 2005-01-28 12:20:52 得分 0
typedef void (*EVENT)();
class BaseClass
{
public:
EVENT ev1;
EVENT ev2;
};
class Clent1 : public BaseClass
{
public:
void funcation1()
{
ev1();
}
};
class Clent2 : public BaseClass
{
public:
Clent1 obj1;
static void funcation()
{
printf("welcome");
};
Clent2()
{
obj1.ev1 = &Clent2::funcation;
obj1.funcation1();
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Clent2();
return 0;
}Top
5 楼QXK2001(Q龙)回复于 2005-01-28 12:33:38 得分 0
static 是可以,但是有个问题,就是不能访问成员函数和成员变量,这样的话就失去意义了,不如直接定义普通函数Top
6 楼wangbo1118(编程浪子)回复于 2005-01-28 13:06:23 得分 0
可以用策略模式嘛Top
7 楼caocheng8230(学C++而不知疲倦)回复于 2005-01-28 13:11:34 得分 0
可以用stl中的一个函数memfunc好像有类似的功能
Top
8 楼QXK2001(Q龙)回复于 2005-01-28 14:59:39 得分 0
策略模式是?Top
9 楼blueskyzsz(青禾)回复于 2005-01-28 15:01:18 得分 20
// 不对的地方自己改
// vc6 下通过 ^_^
#pragma warning ( disable : 4786)
#include <iostream>
#include <map>
#include <assert.h>
using namespace std;
class base
{
public:
typedef void (base::*typefun)();
typedef map<unsigned int, typefun> type_event_map;
void AddEvent(unsigned int unStatus_event, typefun typeFunArgu)
{
m_mapFun.insert(make_pair(unStatus_event, typeFunArgu));
}
type_event_map& GetEventMap()
{
return m_mapFun;
}
protected:
type_event_map m_mapFun;
};
class Clent1 : public base
{
public:
void EventFunc(base& rBase, unsigned int unStatus)
{
type_event_map& rEventMap = rBase.GetEventMap();
typefun pFun = rEventMap[unStatus];
(rBase.*pFun)();
}
};
class Clent2 : public base
{
public:
Clent2(unsigned int unArgu = unsigned int())
{
m_unCount = unArgu;
}
// Event function begin
void EventFun0()
{
cout << "EventFun0 call" << " " << "value: " << m_unCount << endl;
++m_unCount;
}
void EventFun1()
{
cout << "EventFun1 call" << " " << "value: " << m_unCount << endl;
++m_unCount;
}
void EventFun2()
{
cout << "EventFun2 call" << " " << "value: " << m_unCount << endl;
++m_unCount;
}
void EventFun3()
{
cout << "EventFun3 call" << " " << "value: " << m_unCount << endl;
++m_unCount;
}
// Event function end
void AddAllEvent()
{
AddEvent(0, (typefun)EventFun0);
AddEvent(1, (typefun)EventFun1);
AddEvent(2, (typefun)EventFun2);
AddEvent(3, (typefun)EventFun3);
}
void ClentEvent(unsigned int unStatus)
{
m_clent1.EventFunc(*this, unStatus);
}
protected:
Clent1 m_clent1;
unsigned int m_unCount;
};
int main(int argc, char** argv)
{
Clent2 zsz;
zsz.AddAllEvent();
zsz.ClentEvent(0);
zsz.ClentEvent(1);
zsz.ClentEvent(2);
zsz.ClentEvent(3);
system("pause");
return 0;
}
Top
10 楼QXK2001(Q龙)回复于 2005-01-28 15:28:06 得分 0
错误提示如下:
----------------------------------------------------------------
ttt.cpp:41: syntax error before `;' token
ttt.cpp: In member function `void Clent2::AddAllEvent()':
ttt.cpp:70: no matches converting function `EventFun0' to type `void (class
base::*)()'
ttt.cpp:48: candidates are: void Clent2::EventFun0()
ttt.cpp:71: no matches converting function `EventFun1' to type `void (class
base::*)()'
ttt.cpp:53: candidates are: void Clent2::EventFun1()
ttt.cpp:72: no matches converting function `EventFun2' to type `void (class
base::*)()'
ttt.cpp:58: candidates are: void Clent2::EventFun2()
ttt.cpp:73: no matches converting function `EventFun3' to type `void (class
base::*)()'
ttt.cpp:63: candidates are: void Clent2::EventFun3()Top
11 楼blueskyzsz(青禾)回复于 2005-01-28 15:43:32 得分 0
我的系统 vc++6.0 sp6Top
12 楼QXK2001(Q龙)回复于 2005-01-28 15:46:35 得分 0
好像跟系统没有关系吧?它是说不能转换Top
13 楼blueskyzsz(青禾)回复于 2005-01-28 15:54:53 得分 0
在我的机器上编译运行没问题啊,奇怪Top
14 楼QXK2001(Q龙)回复于 2005-01-28 15:55:52 得分 0
还有谁帮忙测试一下呀Top
15 楼hongjun_han(han)回复于 2005-01-29 09:29:00 得分 0
测过了,没问题。
EventFun0 call value: 0
EventFun1 call value: 1
EventFun2 call value: 2
EventFun3 call value: 3
请按任意键继续. . .
Top




