confused with typedefs

E

Eric Sosman

raashid said:
typedef int function(int);

Please explain how can i use this one

There's not much you can do with this one. You can
use it in the declaration of a function pointer:

int this(int x) { return x + 1; }
int that(int y) { return y - 1; }
function *fptr = this;
function *gptr = that;

.... but that's about the extent of it. You cannot use
it in the actual definition of a function taking one int
parameter and returning an int value:

function this { return x + 1; } /* no good */

.... because there's no way the typedef can supply the name
of the parameter (no, not even if you write a name in the
typedef itself).
 
J

jameskuyper

Eric said:
There's not much you can do with this one. You can
use it in the declaration of a function pointer:

You can also use it in a declaration of a function:

function this;
function that;

Note that this can be useful if inserted before definitions like the
ones below, because any mismatch between the function definition and
the function declaration will require a diagnostic message.
int this(int x) { return x + 1; }
int that(int y) { return y - 1; }
....
 
B

Ben Bacarisse

jameskuyper said:
You can also use it in a declaration of a function:

function this;
function that;

and it can be used to declare a function parameter where is will be
adjusted to a pointer type (so this is not really another way to use
it all!):

int call_it(function f, int i) { return f(i); }

This style is odd enough that it should probably be discouraged.

<snip>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,151
Messages
2,570,854
Members
47,394
Latest member
Olekdev

Latest Threads

Top