C++: Difference Between Ampersand "&" and Asterisk "*" in Function/Method Declaration

C++: difference between ampersand & and asterisk * in function/method declaration?

Both do the same, but one uses references and one uses pointers.

See my answer here for a comprehensive list of all the differences.

Difference between the * and & operator in function calls

& function parameter specifically signifies that this parameter is being passed-in by reference (traditionally compilers implement this as a pointer) which is why you see the effect of this assignment in your main().
static would have nothing to do with that.

The difference in declaring a parameter to a function using & and * is that the second one allows a nullptr (or a non-existent or just a plain invalid address) to be passed-in while the & guarantees that there's a real object being referenced by this function's argument. Other than that both provide similar functionality of allowing an original object to be changed via it's reference.

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.

Declaring pointers; asterisk on the left or right of the space between the type and name?

It's a matter of preference, and somewhat of a holy war, just like brace style.

The "C++" style

someType* somePtr;

is emphasizing the type of the pointer variable. It is saying, essentially, "the type of somePtr is pointer-to-someType".

The "C" style

someType *somePtr;

is emphasizing the type of the pointed-to data. It is saying, essentially, "the type of data pointed to by somePtr is someType".

They both mean the same thing, but it depends on if a given programmer's mental model when creating a pointer is "focused", so to speak, on the pointed-to data or the pointer variable.

Putting it in the middle (as someType * somePtr) is trying to avoid committing to either one.

Use of the & operator in C++ function signatures

A reference is not a pointer, they're different although they serve similar purpose.
You can think of a reference as an alias to another variable, i.e. the second variable having the same address. It doesn't contain address itself, it just references the same portion of memory as the variable it's initialized from.

So

string s = "Hello, wordl";
string* p = &s; // Here you get an address of s
string& r = s; // Here, r is a reference to s

s = "Hello, world"; // corrected
assert( s == *p ); // this should be familiar to you, dereferencing a pointer
assert( s == r ); // this will always be true, they are twins, or the same thing rather

string copy1 = *p; // this is to make a copy using a pointer
string copy = r; // this is what you saw, hope now you understand it better.

Parameters by reference to a C function

References are C++ only (not C).

The implementations of the methods will be different, since dereferencing a reference is different from dereferencing a pointer in C++.

e.g.

ref.method();

vs.

ptr->method();

In both cases you're calling on the original object passed to the method, not a copy.



Related Topics



Leave a reply



Submit