0
typedef void(Object Sender) TNotifyEvent;

这就是我试图从 Delphi 到 C++ 所做的事情,但它无法使用 'type int unexpected' 进行编译。

结果应该是我可以这样使用的东西:

void abcd(Object Sender){
  //some code
}

void main{
   TNotifyEvent ne = abcd;
}

我如何制作这种类型(类型为 void)?我对 C++ 不是很熟悉。

4

3 回答 3

4

你想要一个指向一个函数的指针吗?它接受一个对象并且什么都不返回?

 typedef void (*TNotifyEvent)(Object Sender);
于 2010-07-15T15:18:11.613 回答
1

如果您想要定义一个以 Object 作为参数并且不返回任何内容的函数的类型,则语法为:

typedef void TNotifyEvent( Object Sender );

编辑,作为对评论的回答。

是的,您可以定义函数的类型,该类型稍后可以在不同的上下文中使用,具有不同的含义:

TNotifyEvent func;          // function declaration (weird as it might look)
                            // same as: void func( Object Sender );
TNotifyEvent *fp = func;    // function pointer declaration -- initialized with &func
void func( Object Sender )  // cannot use the type when defining the function
{}
void foo( TNotifyEvent f ); // compiler translates to TNotifyEvent * f
                            // just as 'int a[5]' is converted to 'int *a' 
                            // in function parameter lists.
于 2010-07-15T15:18:01.347 回答
1

不,因为 C++ 中没有通用的“对象”类。您可能想要的是一个回调(查看事件...),为此您需要一个仿函数(类重载operator())或函数指针。

下面是一个使用函数指针的例子:

typedef void (*TNotifyEvent)(void *sender);
于 2010-07-15T15:18:55.480 回答