[英]Can i create in-class function types?
I am working on a little project containing simple server which listens to messages. 我正在开发一个包含监听消息的简单服务器的小项目。
Assuming we have the following classes: 假设我们有以下类:
class Message;
class Server
{
void handler1(Message msg);
void handler2(Message msg);
.
.
.
void handler19(Message msg);
};
Can i create a new function type of the class Server
that returns void
and gets a single Message
variable called msg like this: 我可以创建一个返回
void
的类Server
的新函数类型,并获取一个名为msg的Message
变量,如下所示:
typedef void(Server::*MessageHandler)(Message msg);
So the class declaration would be: 所以类声明将是:
class Server
{
MessageHandler handler1;
.
.
.
MessageHandler handler19;
}
A thing as mentioned would come handy with sqlite3 callback functions (where the function declaration is not the cleanest). 所提到的事情对于sqlite3回调函数会很方便(函数声明不是最干净的)。
If it is possible, how can i implement such function? 如果有可能,我该如何实现这样的功能?
If this is not possible, is there any similar way of making the code more simple (like C# delegates)? 如果这是不可能的,有没有类似的方法使代码更简单(如C#代表)?
Thank you in advance! 先感谢您!
For the code: 对于代码:
class Server {
void handler1(Message msg); // This declares a method named handler one which takes a Message and returns void
void(Server::*handler)(Message); // This declares a member variable named handler of the type Server method pointer to a method that takes a Message and returns a void
};
handler
can be reassigned with any Server
method which takes a Message
and returns void
. 可以使用任何接收
Message
并返回void
Server
方法重新分配handler
。 But it must be initialized before it is first used; 但它必须在首次使用之前进行初始化; something like:
handler = handler1
at this point a call to handler
would be a call to handler1
. 类似于:
handler = handler1
此时对handler
的调用将是对handler1
的调用。 But handler
could then be reassigned at will, for example if you did: handler = handler2
at this point a call to handler
would be a call to handler2
. 但是可以随意重新分配
handler
,例如,如果你这样做: handler = handler2
此时对handler
的调用将是对handler2
的调用。
I wasn't sure from your question if you understood, but handler
never defines a method; 我不确定你的问题是否理解,但
handler
从未定义方法; only a pointer to one. 只有指向一个的指针。
Given this definition of Server
: 鉴于
Server
这个定义:
class Server {
void Handler1(int param) { cout << param + 1 << endl; }
void Handler2(int param) { cout << (param << 1) << endl; }
void Handler3(int param) { cout << (param != 0 ? "true\n" : "false\n"); }
void Handler4(int param) { cout << static_cast<char>(param + 'A') << endl; }
public:
function<void(Server, int)> handler_;
void next() {
if (!handler_ || *handler_.target<void (Server::*)(int)>() == &Server::Handler4) {
handler_ = &Server::Handler1;
} else if(*handler_.target<void (Server::*)(int)>() == &Server::Handler1) {
handler_ = &Server::Handler2;
} else if (*handler_.target<void (Server::*)(int)>() == &Server::Handler2) {
handler_ = &Server::Handler3;
} else {
handler_ = &Server::Handler4;
}
}
};
You could do the following: 您可以执行以下操作:
Server foo;
for (auto i = 'A'; i <= 'Z'; ++i) {
foo.next();
foo.handler_(foo, i);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.