Home >Backend Development >C++ >What's the Difference Between Single and Double Ampersands in C Member Function Declarations?
In-depth Interpretation of the Ampersand in Member Function Declarations
In C , non-static member functions can be decorated with ref-qualifiers. These qualifiers specify the reference category of the implicit object parameter that is passed to the function.
Let's explore the two common ref-qualifiers:
Without specifying any ref-qualifier, the function can be invoked regardless of the value category of the object.
To illustrate the difference:
struct Foo { void bar() {} // Default: can be invoked with both lvalues and rvalues void bar1() & {} // Can only be invoked with lvalues void bar2() && {} // Can only be invoked with rvalues };
In the above example:
Here's a live demonstration:
int main() { Foo f; f.bar(); f.bar1(); Foo().bar2(); // Error: bar2 requires an rvalue }
Understanding these ref-qualifiers allows you to control the access to your member functions based on the reference category of the object they are being invoked on.
The above is the detailed content of What's the Difference Between Single and Double Ampersands in C Member Function Declarations?. For more information, please follow other related articles on the PHP Chinese website!