c++和objective c混编,回调函数不能用!!
tanxj 2010-09-25 10:14:10 之前发过一个帖子:http://topic.csdn.net/u/20100924/18/11f895bb-040b-41ee-ac68-9d040ae83995.html
//c++类:
typedef int (*MyMessagePtrType)( int); //回调函数
class ApplicationEngine : public IApplicationEngine {
public:
ApplicationEngine(MyMessagePtrType func);
~ApplicationEngine();
......
private:
......
MyMessagePtrType my_message_ptr;
};
IApplicationEngine* CreateApplicationEngine(MyMessagePtrType func)
{
return new ApplicationEngine(func);
}
ApplicationEngine::ApplicationEngine(MyMessagePtrType func):
{
my_message_ptr = func;
}
void ApplicationEngine::OnFingerDown(vec2 location, float elapsed)
{
int msg = 4;
if (my_message_ptr)
(*my_message_ptr)(msg); //这里直接用(*my_message_ptr)(4);的话,参数4送不过去。
//另外,用my_message_ptr(msg)的话,一样结果,不知道为什么还要加*??
}
objective-c 中的代码:
@interface GLView : UIView {
@private
BOOL m_pauseRender;
}
- (void) drawView: (CADisplayLink*) displayLink;//这个是objective-c类的一个回调
- (int) OnOpenGLMessage: (id) event Message: (int) msg; //这里的(id) fid 是自己在测试后加的,
//如果用- (int) OnOpenGLMessage: (int) msg;的话得不到传送过来的参数
@end
.....
CADisplayLink* displayLink = [CADisplayLink displayLinkWithTarget:self
selector:@selector(drawView:)];//objective-c要送sel,不是送函数指针。(这个方便!)
m_applicationEngine = CreateApplicationEngine(
(MyMessagePtrType)[self methodForSelector:@selector(OnOpenGLMessage:Message:)]);//给函数指针到c++类
.....
- (void) drawView: (CADisplayLink*) displayLink //这个一点问题没有。
{
if (m_pauseRender) return;
......
}
- (int) OnOpenGLMessage: (id)event Message:(int) msg
{
NSLog(@"\n回调 %d\n", msg); //可以显示结果
switch (event) {
case 4:
m_pauseRender = YES;//程序在这里死掉
return 4;
break;
default:
break;
}
retrun 0; //不是4的话可以正常返回0
}
回调函数都没问题,就是在回调函数中使用GLView类中的成员时就死掉,到低是哪里没定义好还是其它问题。望各位大哥帮帮忙看看,万分感谢!!!
上次帖子中的zenny_chen兄,这次再要麻烦你了!!!!