What is the meaning of & in C ++?

I want to know the meaning in the example below:

class1 &class1::instance(){

///something to do

}
+3
source share
6 answers

This means that your method returns a reference to the method1 object. A link is like a pointer that it refers to an object, not a copy of it, but the difference with the pointer is that the links are:

  • can never be undefined / null
  • you cannot do pointer arithmetic with them

Thus, it is a kind of lightweight, safer option for pointers.

+7
source

The operator &has three meanings in C ++.

  • Bitwise AND, for example. 2 & 1 == 3
  • Address from, for example: int x = 3; int* ptr = &x;
  • A reference type modifier, for example. int x = 3; int& ref = x;

. class1 &class1::instance() - class1, instance, reference -to- class1, , class1& class1::instance() ( ).

+7

( ) .

+1

, .

+1

In the context of the statement, it looks like it will return a reference to the class in which it was defined. I suspect there is a "Do Stuff" section

return *this;
0
source

This means that the variable is not the variable itself, but a reference to it. Therefore, if you change the value, you will see it immediately if you use the print statement to view it. Look at the links and pointers for a more detailed answer, but basically it means a reference to a variable or object ...

0
source

All Articles