回调函数问题 [英] Callback functions problem

查看:102
本文介绍了回调函数问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello ther

我在某地读过回调函数应该是全局的还是静态的

成员函数。

1:我可以使用静态成员函数,但我怎么能访问成员

类不是静态的。

2:我可以使用全局函数但想要访问类的成员。

让我知道可能的解决方案。


问候,

Ajay Sonawane

解决方案



< aj *********** @ gmail.com>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...

Hello ther
我在某地读过回调函数应该是全局的还是静态的
成员函数。
1:我可以使用静态成员函数但是如何访问成员呢
非静态的类。
2:我可以使用全局功能,但想要访问班级成员。
让我知道可能的解决方案。

问候, Ajay Sonawane



我使用过的大多数回调函数都提供了一个void *指针,通常是一个名为userData或类似的
,这就是你可以像你一样使用

。我所做的就是编写一个全局回调函数,并输入

void * thethis想要接收回调的对象的指针。

然后,在回调中,我只是将void *指针转换为指向我的

类的指针,然后使用它调用该对象上的成员函数来执行回调的实际工作。


-Howard


您可以尝试使用友元功能。这将为您提供全局功能

,也可以访问会员数据。


< aj *********** @ gmail .COM>在消息中写道

news:11 ********************** @ o13g2000cwo.googlegr oups.com ...

Hello ther
我在某地读过回调函数应该是全局的还是静态的
成员函数。
1:我可以使用静态成员函数但是如何访问成员呢
非静态的类。
2:我可以使用全局功能,但想要访问班级成员。
让我知道可能的解决方案。

问候, Ajay Sonawane





" kloodge" < mg@aol.com>在消息中写道

新闻:mJ ***************** @ tornado.texas.rr.com ...


请不要顶尖。 [重新安排]

< aj *********** @ gmail.com>在消息中写道
新闻:11 ********************** @ o13g2000cwo.googlegr oups.com ...

你好,我在某地读过回调函数应该是全局的还是静态的
成员函数。
1:我可以使用静态成员函数但是我怎样才能访问类的成员
不是静止的。
2:我可以使用全局功能,但想要访问班级成员。
让我知道可能的解决方案。

问候,
Ajay Sonawane



您可以尝试使用友元功能。这将为您提供一个全局功能
,也可以访问会员数据。




虽然友情功能允许您访问对象的私人或

受保护的成员,在这种情况下它没有帮助,因为静态成员或

全局函数没有该类的实例来操作。

的问题是关于访问非静态成员,这意味着他需要一个

类的实例来操作。友谊只会影响能见度。


-Howard


Hello ther
I read somewhere that callback function should be global or static
member function.
1: I could use static member functions but how could I access members
of class which ar not static.
2: I could use global function but want to access members of class.
Let me know the possible solutions.

Regards,
Ajay Sonawane

解决方案


<aj***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...

Hello ther
I read somewhere that callback function should be global or static
member function.
1: I could use static member functions but how could I access members
of class which ar not static.
2: I could use global function but want to access members of class.
Let me know the possible solutions.

Regards,
Ajay Sonawane


Most callback functions that I''ve worked with provide a void* pointer, often
called userData or something similar, which is there for you to use as you
see fit. What I do is write a global callback function, and put in that
void* the "this" pointer of the object which wants to receive the callbacks.
Then, in the callback, I simply cast the void* pointer to a pointer to my
class, and use it to call a member function on that object to do the actual
work of the callback.

-Howard


You may try using a friend function. This would give you a global function
that can also access member data.

<aj***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...

Hello ther
I read somewhere that callback function should be global or static
member function.
1: I could use static member functions but how could I access members
of class which ar not static.
2: I could use global function but want to access members of class.
Let me know the possible solutions.

Regards,
Ajay Sonawane




"kloodge" <mg@aol.com> wrote in message
news:mJ*****************@tornado.texas.rr.com...

Please don''t top-post. [Re-arranged]

<aj***********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...

Hello ther
I read somewhere that callback function should be global or static
member function.
1: I could use static member functions but how could I access members
of class which ar not static.
2: I could use global function but want to access members of class.
Let me know the possible solutions.

Regards,
Ajay Sonawane


You may try using a friend function. This would give you a global function
that can also access member data.



While a friend function will allow you access to an object''s private or
protected members, it does not help in this case, because a static member or
global function does not have an instance of that class to operate on. The
question was about accessing non-static members, which means he needs an
instance of the class to operate on. Friendship only affects visibility.

-Howard


这篇关于回调函数问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆