2

Found this example and wonder what the & before the closing bracket does?

void f(const Foo &)
{
    std::cout << "f(const Foo&)\n";
}

Example taken from here

Help will be much appreciated.

user3139868
  • 393
  • 2
  • 12

3 Answers3

2

In C++, if you don't use a parameter, you don't have to name it. Some compiler settings even generate warnings if you do name them. An unnamed parameter only consists of a type and the type can also be a pointer or a reference or combinations of them. In this case (as already answered), the type is a reference.

Unused parameters are mostly useless and should be avoided, but they can exist in virtual functions, where some implementations need them and others don't.

stefaanv
  • 14,072
  • 2
  • 31
  • 53
  • Thank you very much. I hadn't come across unnamed parameters in C++ yet. You helped clear that up pretty neatly. – user3139868 Mar 25 '15 at 17:34
  • I also found a [post](http://stackoverflow.com/questions/12186698/on-unnamed-parameters-to-functions-c) explaining a bit about unnamed parameters - for all those that might marvel at them as I do. – user3139868 Mar 25 '15 at 17:39
1

Its a reference to type Foo. You should get yourself a C++ book and read through it.

http://en.wikipedia.org/wiki/Reference_%28C%2B%2B%29

Mercious
  • 378
  • 3
  • 25
1

In simple terms the & says it's a reference.

So when you use the & operator in the function declaration, preceded by a type(here it is Foo) it means "reference to" which is essentially an automatically dereferenced pointer with disabled pointer arithmetic.

Also check this reference for details

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331