English 中文(简体)
• 采用一种 style背目标?
原标题:Making a C style callback object oriented?

我用一个像这样的图书馆:

void onReceive (Lacewing::Server &Server, Lacewing::Server::Client &Client,
                char * Data, int Size) {
  /* callback body */
}

Server.onReceive (onReceive); /* to register the handler */

我愿以一个能够决定何时收到包装单(观察器型号)的类别来总结一下。

我怎么能用回头来这样做? 图书馆没有界定继承的接口。

成就

最佳回答

自您重新使用校正以来,每组都有一个<条码>,即:

/* class method */

void MyServer::onReceive (Lacewing::Server &Server, Lacewing::Server::Client &Client,
            char * Data, int Size)
{
     /* callback body - this is inside the class */
}


/* global function wraps the class method */

void onReceive (Lacewing::Server &Server, Lacewing::Server::Client &Client,
            char * Data, int Size)
{
    ((MyServer *) Server.Tag)->onReceive (Server, Client, Data, Size);
}

之后:

Server.Tag = myServerInstance; /* set the class instance pointer */
Server.onReceive (::onReceive); /* register the global function */
问题回答

如果我正确理解你的话,你可以采取两种不同的方式。

假设这一反馈结构正在使用数据参数,使你能够把自己的数据传递给反馈职能(共同模式),你可以这样做:

class MyProcessingClass
{
public:
    MyProcessingClass();
    virtual ~MyProcessingClass();

    // Do whatever processing in this method
    virtual void onReceive(Lacewing::Server &Server, Lacewing::Server::Client &Client);
}

void onReceive (Lacewing::Server &Server, Lacewing::Server::Client &Client,
                char * Data, int Size)
{
    if ( Data != NULL )
    {
        MyProcessingClass *handler = reinterpret_cast<MyProcessingClass *>(Data);
        handler->onReceive( Server, Client );
    }
}

或者,如果数据点是你需要处理的,而不是“用户数据点”,那么你可能不得不使用单一吨、某种全球变数或类似数据。 在我的经验中,这是使用反馈的不太常见的方法,希望是你重新处理。

MyProcessingClass g_Processor;

MyProcessingClass *GetProcessor()
{
    return &g_Processor; // or some other way of getting your instance
}

void onReceive (Lacewing::Server &Server, Lacewing::Server::Client &Client,
                char * Data, int Size)
{
    MyProcessingClass *handler = GetProcessor();
    if ( handler != NULL )
    {
        handler->onReceive( Server, Client );
    }
}




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签