reference to a function

M

Mark

can someone tell me what the following does

typedef int(&rifii)(int,int)

I'm used to pointers to functions, but am not sure what
the above is used for?

Thanks
Mark
 
M

Micah Cowan

Mark said:
can someone tell me what the following does

typedef int(&rifii)(int,int)

I'm used to pointers to functions, but am not sure what
the above is used for?

Thanks
Mark

It's used for pretty much the same things as pointers to
functions, except that it cannot be reassigned. I can't really
think of many uses for this, except that the initialization might
be performed using a function such as:

int foo(int,int);
int bar(int,int);
enum Func_choose {
use_foo,
use_bar
};

rifii initialize_rifii(Func_choose fc)
{
if (fc == use_foo)
return foo;
else
return bar;
}

HTH.
 
R

Rob Williscroft

Mark wrote in in comp.lang.c++:
can someone tell me what the following does

typedef int(&rifii)(int,int)

It defines an alias (typedef) for a reference to a function type.
I'm used to pointers to functions, but am not sure what
the above is used for?

Use it like an other reference/typedef:

int f(int, int) { return 0;}
int g( rifii func )
{
return func();
}

int main()
{
return g(f);
}

The only advantage over a pointer is that a reference can't
be "reseated".

Rob.
 

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

Members online

No members online now.

Forum statistics

Threads
474,197
Messages
2,571,038
Members
47,633
Latest member
BriannaLyk

Latest Threads

Top