typedef'ing function * signature *(不是指针),以便它可以重用? [英] typedef'ing function *signature* (not a pointer to), so that it can be reused?

查看:238
本文介绍了typedef'ing function * signature *(不是指针),以便它可以重用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

EDIT:MOTIVATION



假设我将一个Handler类定义为

  class Handler {
public:
class Message {/*...*/};
typedef int(* Callback)(Message * msg);
void registerCallback(int msgclass,Callback f);
};

客户可以执行

  int f1(Handler :: Message * msg)
{/ * handle message * /}

int f2(Handler :: Message * msg)
{/ * handle message * /}

int main(){
Handler h;
h.registerCallback(1,f1);
h.registerCallback(2,f2);
// ...
}

编译器会检查 f1 f2 适合作为 registerCallback 的参数,由客户端正确定义 f1 f2 因为我已经已经 typedef ed 回调,我想客户端能够使用它。 / p>

END EDIT



我想这样做: p>

  typedef int arithmetic(int i,int j); 

算术和
{
return i + j;
}

算术max
{
return(i> j)? i:j;
}
//等

但是,

 算术和
算术sum()

不编译,以及此

 算术sum(int i,int j)

这会导致

的编译错误


func.cpp:4:错误:'sum'声明为
函数返回一个函数


原因我想这是我想要一个处理程序类,将提供 typedef 为它接受的回调函数,包括参数列表

解决方案

签名是标识单个函数的一切。它包含函数的命名空间/类,等等。



你键入的是函数的类型。类似于当你 typedef int inttype ,它typedefs一个 int 的类型,你typedef'ed一个函数的类型。



您可以使用typedef名称来声明函数。

  arithmetic max; // valid 

但它不能用于定义函数。对于定义函数,您需要逐字地和手动地提供参数列表。原因包括给参数命名(可能还有其他更技术的原因C ++ 0x引入了 arithmetic max {}; ,这将得到一个特定的初始化含义)。


EDIT: MOTIVATION

Suppose I define a Handler class as

class Handler {
public:
  class Message { /*...*/ };
  typedef int (*Callback)(Message *msg);
  void registerCallback(int msgclass, Callback f);
};

A client can do

int f1(Handler::Message *msg)
{ /* handle message */ }

int f2(Handler::Message *msg)
{ /* handle message */ }

int main(){
  Handler h;
  h.registerCallback(1, f1);
  h.registerCallback(2, f2);
  // ....
}

The compiler will indeed check that f1 and f2 are appropriate as parameters to registerCallback, however, it's up to the client to define f1 and f2 correctly. Since I've allready typedefed Callback, I'd like the client to be able to use it instead.

END EDIT

I'd like to do something like this:

typedef int arithmetic(int i, int j);

arithmetic sum
{
 return i+j;
}

arithmetic max
{
  return (i>j)? i:j;
}
// etc.

However, both

arithmetic sum
arithmetic sum()

don't compile, and also this

arithmetic sum(int i, int j)

which gives compiler error of

func.cpp:4: error: ‘sum’ declared as function returning a function

The reason I want this is that I want to have a Handler class which would provide a typedef for a callback function it accepts, including the parameter list.

解决方案

First, you did not typedef a signature. A signature is everything that identifies a single function. It contains the namespace/class of the function and so on.

What you typedef'ed is the type of a function. Like when you typedef int inttype which typedefs the type of an int, you typedef'ed the type of a function.

You can use the typedef-name to declare functions only.

arithmetic max; // valid

But it cannot be used to define functions. For defining functions, you need to provide a parameter list literally and manually. Reasons include giving names for parameters (and possibly other, more technical reasons. C++0x introduces arithmetic max {}; which will get a specific initialization meaning).

这篇关于typedef'ing function * signature *(不是指针),以便它可以重用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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