C ++中的回调 [英] Callbacks in C++

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

问题描述

我需要在C ++中使用回调。


我从Rich Hickey找到了很棒的文章,我正在使用他的回调。
$ b $我的项目中的b文件。问题是,当我编译我的项目时,我得到两页警告:''隐式typename已被弃用''。有没有人

有固定版本的callback.h,还是有人知道如何关闭

这个警告?


另外使用类成员作为回调函数的任何其他方法都非常有用。


Simon Toth

Hi, i need to use callbacks in C++.

I found great article from Rich Hickey, and i''m using his callback.h
file in my project. The problem is that when I compile my project I get
two pages of warnings: ''implicit typename is deprecated''. Does anybody
have fixed version of callback.h, or does anybody know how to turn off
this warning?

Also any other methods for using class members as callback functions are
greatly apreciated.

Simon Toth

推荐答案



"SimonTóth" <硅**** @ Mail.Muni.cz>在消息中写道

news:IF ******** @ news.muni.cz ...

"Simon Tóth" <Si****@Mail.Muni.cz> wrote in message
news:IF********@news.muni.cz...
我需要在C ++中使用回调。<我找到了来自Rich Hickey的精彩文章,我正在我的项目中使用他的callback.h文件
。问题是当我编译我的项目时,我得到两个警告页:''隐式typename被弃用''。有没有人有固定版本的callback.h,或者有人知道如何关闭这个
警告吗?

使用类成员作为回调函数的任何其他方法都是<很大的偏见。

Simon Toth
Hi, i need to use callbacks in C++.

I found great article from Rich Hickey, and i''m using his callback.h file
in my project. The problem is that when I compile my project I get two
pages of warnings: ''implicit typename is deprecated''. Does anybody have
fixed version of callback.h, or does anybody know how to turn off this
warning?

Also any other methods for using class members as callback functions are
greatly apreciated.

Simon Toth




由于我们大多数人不太可能拥有该特定文件,您可能需要

至少发布导致错误的部分内容。猜测确切地说是什么构造导致了特定的错误信息真是太难了。


至于回调的方法,我用了两个:一个是使用静态成员

函数,另一个是使用非成员函数。在这两种情况下,

回调都有一个(void *)指针,它获取this的值。当我设置注册时,指针

回调,我只是投射到对象

类型我知道它在我的回调中,并用它来调用非静态的

成员函数来做我的实际工作。 (但我不熟悉你正在讨论的

,至少不是那些有限的信息。)


-Howard



Since most of us are not likely to have that particular file, you might want
to post at least a part of it that''s causing the error. It''s kind of hard
to guess exactly what construct causes that specific error message.

As for methods of callbacks, I''ve used two: one is to use a static member
function, and the other is to use a non-member function. In both cases, the
callback has a (void*) pointer which gets the value of the "this" pointer
when I set "register" the callback, and which I simply cast to the object
type I know it to be inside my callback, and use that to call a non-static
member function to do the actual work for me. (But I''m not familar with the
one you''re discussing, at least not with that limited info.)

-Howard


?* imon T?3th写道:


pseudoi需要在C ++中使用回调。
?*imon T?3th wrote:

pseudoi need to use callbacks in C++.

我从Rich Hickey找到了很棒的文章,我在我的项目中使用了他的callback.h
文件。问题是当我编译我的项目时,我得到两页警告:''隐式typename被弃用''。有没有人有固定版本的callback.h,或者有人知道如何关闭
这个警告吗?

使用类成员作为回调函数的任何其他方法都是<很大的偏见。

Simon Toth

I found great article from Rich Hickey, and i''m using his callback.h
file in my project. The problem is that when I compile my project I get
two pages of warnings: ''implicit typename is deprecated''. Does anybody
have fixed version of callback.h, or does anybody know how to turn off
this warning?

Also any other methods for using class members as callback functions are
greatly apreciated.

Simon Toth




我使用多态方法。它使用类成员作为

回调函数,但不是以相同的方式。


我创建一个抽象类(下面的WorkerInterface),包含一个或

更纯的虚拟方法。然后我的另一个类(下面的MyClass)继承了

WorkerInterface并实现了纯虚方法。


例如(这不会编译..它只是得到了点跨越)


//接口

类WorkerInterface

{

public:

...

void worker_callback()= 0;

}


//执行内容的对象拨打电话。

班级工人

{

公共:

...

//将l添加到要调用的列表中

void add_listener(WorkerInterface * l);


//删除l

void remove_listener(WorkerInterface * l);

private:

//调用纯虚函数

fire_callback()

{

for(int i = 0; i< 5; i ++)

if(listeners [i]!= NULL)

listeners [i] .worker_callback();

}


//工人可以有5个注册的听众

WorkerInterface听众[5];

}


class MyClass:public WorkerInterface

{

public:

MyClass(int i):n(i){worker.add_listener(this);}

void worker_callback()

{

cout<< MyClass << n<< "被称为 <<结束;

}


工人工人;

int n;

}

-

Alvin



I use the polymorphism approach. It sort of uses a class member as a
callback function, but not in the same fashion.

I create an abstract class (WorkerInterface below), which contains one or
more pure virtual methods. Then my other class (MyClass below) inherits
WorkerInterface and implements the pure virtual method.

For example (this wont compile..it just gets the point across)

// Interface
class WorkerInterface
{
public:
...
void worker_callback() = 0;
}

// Object that does stuff and calls the callback.
class Worker
{
public:
...
// adds l to the list to be called
void add_listener(WorkerInterface *l);

// removes l
void remove_listener(WorkerInterface *l);
private:
// calls the pure virtual function
fire_callback()
{
for(int i = 0; i < 5; i++)
if(listeners[i] != NULL)
listeners[i].worker_callback();
}

// Worker can have 5 registered listeners
WorkerInterface listeners[5];
}

class MyClass : public WorkerInterface
{
public:
MyClass(int i) : n(i) {worker.add_listener(this);}
void worker_callback()
{
cout << "MyClass " << n << " was called" << endl;
}

Worker worker;
int n;
}
--
Alvin


>由于我们大多数人不太可能拥有该特定文件,因此您可能希望
> Since most of us are not likely to have that particular file, you might want
至少发布导致错误的部分内容。准确地猜测构造导致特定错误消息是很难的。
to post at least a part of it that''s causing the error. It''s kind of hard
to guess exactly what construct causes that specific error message.



正如我写的那样,它是来自Rich Hickey的callback.h。我现在无法找到链接。


我只需要转动愚蠢的隐式类型名称已被弃用,

请参阅文档了解详细信息警告。


As I wrote, it''s callback.h from Rich Hickey. I cant find the link now.

I just need to turn of the stupid "implicit typename is deprecated,
please see the documentation for details" warning.


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

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