In C++, What Does & Mean After a Function's Return Type

In C++, what does & mean after a function's return type?

Yes, the int& version returns a reference to an int. The int version returns an int by value.

See the section on references in the C++ FAQ

How does ampersand in the return type of a function declaration work?

When the & operator is used in a declaration form, preceded by a type it doesn't mean "the address of" but a "reference to" which is essentially an automatically dereferenced pointer with disabled pointer arithmetic.

There are no references in C, so if you want to pass or return by reference, you had to pass a const pointer and dereference it to access the pointed to value. C++ added references to make this concept easier, to prevent accidental walking off the address with pointer arithmetic, and to save the need to dereference the pointer. This makes working with it much easier and resulting in cleaner and more readable syntax.

Use of '&' operator before a function name in C++

In C++, when the ref-sign (&) is used before the function name in the declaration of a function it is associated with the return value of the function and means that the function will return by reference.

int& foo(); // Function will return an int by reference.

When not used within a declaration context, putting the ref-sign before a function name results in calling the address-of operator returning the address of the function. This can be used to e.g. create a pointer to a function.

// Some function.
int sum(int a, int b) {
return a + b;
}

int main() {
// Declare type func_ptr_t as pointer to function of type int(int, int).
using func_ptr_t = std::add_pointer<int(int, int)>::type;

func_ptr_t func = ∑ // Declare func as pointer to sum using address-of.
int sum = func(1, 2); // Initialize variable sum with return value of func.
}

In C, the only use of & is for the address-of operator. References does not exist in the C language.

What does '&' do in a C++ declaration?

The "&" denotes a reference instead of a pointer to an object (In your case a constant reference).

The advantage of having a function such as

foo(string const& myname) 

over

foo(string const* myname)

is that in the former case you are guaranteed that myname is non-null, since C++ does not allow NULL references. Since you are passing by reference, the object is not copied, just like if you were passing a pointer.

Your second example:

const string &GetMethodName() { ... }

Would allow you to return a constant reference to, for example, a member variable. This is useful if you do not wish a copy to be returned, and again be guaranteed that the value returned is non-null. As an example, the following allows you direct, read-only access:

class A
{
public:
int bar() const {return someValue;}
//Big, expensive to copy class
}

class B
{
public:
A const& getA() { return mA;}
private:
A mA;
}
void someFunction()
{
B b = B();
//Access A, ability to call const functions on A
//No need to check for null, since reference is guaranteed to be valid.
int value = b.getA().bar();
}

You have to of course be careful to not return invalid references.
Compilers will happily compile the following (depending on your warning level and how you treat warnings)

int const& foo() 
{
int a;

//This is very bad, returning reference to something on the stack. This will
//crash at runtime.
return a;
}

Basically, it is your responsibility to ensure that whatever you are returning a reference to is actually valid.

meaning of &variable (passed to function)

func being some arbitrary user defined function

It couldn't be "arbitrary" - it must take a pointer to int or a void* in order for the call to be legal.

This ampersand is the "take address" operator. It passes func the address of a, so that the func could, for example, modify it:

void func(int *pa) {
*pa = 4; // Note the asterisk - it "undoes" the effect of the ampersand
}

If your main prints a after the call to func, it prints 4 instead of 3.

Note that if you pass a instead of a pointer to a to a function that takes an int, not an int*, then modifications done to that int inside the function will have no effect on the parameter that you pass, because in C parameters are passed by value.

the variable a in the actual code is probably global or extern or something

It is probably not global, because there is no point in passing globals around: by virtue of being global, they are already accessible from everywhere.



Related Topics



Leave a reply



Submit