10

Possible Duplicate:
Function References

I found this code in a book ,

typedef int (&foo)(int,int);

What does it mean ? Is this reference to a function ? If yes, Why is it used for ? And how is it different from pointer to a function like this .

typedef int (*foo)(int,int);

Is this just an enhancement in C++ which is not in C ?

EDIT :

#include <iostream>
#include <typeinfo>
using namespace std;
int f (int , int ) {return 0;}
int main() {
    int (&foo)(int,int) = f;
    int (*fp)(int,int) = f;
    std::cout<<typeid(foo).name()<<std::endl<<typeid(fp).name();
    return 0;
}

When I print type of foo and fp it gives something like this :

FiiiE
PFiiiE

What is E in the last. and why foo is F while fp is PF ?

Community
  • 1
  • 1
Omkant
  • 9,018
  • 8
  • 39
  • 59
  • my C/C++ powers are weak, however doesnt the "&" approximate to "contents of" and "*" mean "address of" ? .. Just Read the replies to my comment - thanks for that guys, more reading needed. – FreudianSlip Dec 29 '12 at 07:41
  • @FreudianSlip, Not in this context. & can be either that (except you mixed them up), a bitwise and, or a reference. – chris Dec 29 '12 at 07:41
  • @FreudianSlip In C++, `&` can also be used as a type qualifier to declare a reference type. –  Dec 29 '12 at 07:41
  • 3
    Like all references, you can't (easily) *not* set it. Its a reference, and just like all references, once set, it isn't gonna change. A pointer on the other hand... I think you'd be best served to study pointers vs references in C++ *first*. – WhozCraig Dec 29 '12 at 07:41
  • This might help. http://stackoverflow.com/questions/1591361/understanding-typedefs-for-function-pointers-in-c-examples-hints-and-tips-ple – NeonGlow Dec 29 '12 at 07:42
  • C++ enhancements for C almost always are not in C. – Mark Garcia Dec 29 '12 at 07:46
  • @WhozCraig : Yeah I am new to C++ , but wanted to know Is it same as we use references in case of variables or something else – Omkant Dec 29 '12 at 07:47
  • 1
    @Omkant no worries at all, I've seen your posts and answers, and was a little surprised this one took you back a minute. H2CO3's linked article is a good read, btw. – WhozCraig Dec 29 '12 at 07:51
  • Btw edited my question though .. What have I tried – Omkant Dec 29 '12 at 07:59

0 Answers0