tags:

views:

88

answers:

3
typedef void(Object Sender) TNotifyEvent;

This is what I'm trying to do from Delphi to C++, but it fails to compile with 'type int unexpected'.

The result should be something I can use like that:

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

void main{
   TNotifyEvent ne = abcd;
}

How do I make such type(of type void)? I'm not very familiar with C++.

+1  A: 

If what you want is to define the type of a function that takes an Object as parameter and returns nothing, the syntax would be:

typedef void TNotifyEvent( Object Sender );

EDIT, as answer to the comment.

Yes, you can define the type of a function, and that type can later be used in different contexts with different meanings:

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.
David Rodríguez - dribeas
um... Can that be used? C++ can't have a type that's a function, just a pointer to a function, right?
James Curran
I wonder what the problem is with the question --and hence the downvote. @James Curran, yes you can.
David Rodríguez - dribeas
Actually, it can. There are function types in C++, there are just no variables of function types. But eg. what you put as an argument for std::function are actually function types. See here: http://ideone.com/fsn3O and here http://ideone.com/s8oY9
jpalecek
+4  A: 

Do you want a pointer to a function which takes an Object and returns nothing?

 typedef void (*TNotifyEvent)(Object Sender);
James Curran
+1  A: 

No, because there is no common "object" class in C++. What you probably want there is a callback (Looking at events...), for which you need a functor (a class overloading operator()) or a function pointer.

Here's an example using the function pointer:

typedef void (*TNotifyEvent)(void *sender);
Billy ONeal
You can define an Object class, and have your other classes inherit from it. It's not idiomatic C++, but you can do it. It won't cover any system-defined or outside classes, of course.
David Thornley