Where Ampersand "&" Can Be Put When Passing Argument by Reference

Where ampersand & can be put when passing argument by reference?

Both are exactly the same. No difference at all.

All that matters is that & should be between the type and the variable name. Spaces don't matter.

So

void AddOne(int&  y);
void AddOne(int &y);
void AddOne(int & y)
void AddOne(int & y);
void AddOne(int&y);

are same!

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.

Pointers in C: when to use the ampersand and the asterisk?

You have pointers and values:

int* p; // variable p is pointer to integer type
int i; // integer value

You turn a pointer into a value with *:

int i2 = *p; // integer i2 is assigned with integer value that pointer p is pointing to

You turn a value into a pointer with &:

int* p2 = &i; // pointer p2 will point to the address of integer i

Edit:
In the case of arrays, they are treated very much like pointers. If you think of them as pointers, you'll be using * to get at the values inside of them as explained above, but there is also another, more common way using the [] operator:

int a[2];  // array of integers
int i = *a; // the value of the first element of a
int i2 = a[0]; // another way to get the first element

To get the second element:

int a[2]; // array
int i = *(a + 1); // the value of the second element
int i2 = a[1]; // the value of the second element

So the [] indexing operator is a special form of the * operator, and it works like this:

a[i] == *(a + i);  // these two statements are the same thing

how does the ampersand(&) sign work in c++?

To start, note that

this

is a special pointer ( == memory address) to the class its in.
First, an object is instantiated:

CDummy a;

Next, a pointer is instantiated:

CDummy *b;

Next, the memory address of a is assigned to the pointer b:

b = &a;

Next, the method CDummy::isitme(CDummy ¶m) is called:

b->isitme(a);

A test is evaluated inside this method:

if (¶m == this) // do something

Here's the tricky part. param is an object of type CDummy, but ¶m is the memory address of param. So the memory address of param is tested against another memory address called "this". If you copy the memory address of the object this method is called from into the argument of this method, this will result in true.

This kind of evaluation is usually done when overloading the copy constructor

MyClass& MyClass::operator=(const MyClass &other) {
// if a programmer tries to copy the same object into itself, protect
// from this behavior via this route
if (&other == this) return *this;
else {
// otherwise truly copy other into this
}
}

Also note the usage of *this, where this is being dereferenced. That is, instead of returning the memory address, return the object located at that memory address.

What's the semantically accurate position for the ampersand in C++ references

While researching for this question, I already found the answer:

The & needs to be written just like the *.

The demonstration code is similar to the pointer demonstration code:

int main() {
int a = 0;
int b = 1;

int& ar = a, br = b;

br = 2;

return b;
}

This returns 1, which means that ar is an int reference, while br is just an integer.

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.

C++ functions: ampersand vs asterisk

Pointers (ie. the '*') should be used where the passing "NULL" is meaningful.
For example, you might use a NULL to represent that a particular object needs to be created, or that a particular action doesn't need to be taken.
Or if it ever needs to be called from non-C++ code. (eg. for use in shared libraries)

eg. The libc function time_t time (time_t *result);

If result is not NULL, the current time will be stored. But if result is NULL, then no action is taken.

If the function that you're writing doesn't need to use NULL as a meaningful value then using references (ie. the '&') will probably be less confusing - assuming that is the convention that your project uses.



Related Topics



Leave a reply



Submit