Home >Backend Development >C++ >What are Reference Qualifiers (& and &&) in C Member Functions and How Do They Affect Object References?
Reference Qualifiers in Member Function Declarations
In C , single ampersand (&) and double ampersands (&&) can be used as reference qualifiers in member function declarations. These qualifiers affect the type of the implicit object parameter for non-static member functions.
Single Ampersand (Single Reference)
The single ampersand (&) indicates that the member function can be invoked when the object is an lvalue reference (that is, it refers to an existing object). In other words, the function is called with an object that is passed by reference.
The following example demonstrates the use of the single reference qualifier:
class wrap { public: operator obj() const & { ... } // Copy from me. };
Here, the operator obj() function can be invoked on an object reference such as my_object.operator obj().
Comparison with No Ampersand
If no reference qualifier is specified, the implicit object parameter is an lvalue reference by default. Therefore, the following function declaration is equivalent to the one above:
class wrap { public: operator obj() const { ... } // Copy from me. };
Additional Notes
The above is the detailed content of What are Reference Qualifiers (& and &&) in C Member Functions and How Do They Affect Object References?. For more information, please follow other related articles on the PHP Chinese website!