C + + left and right values

Source: Internet
Author: User

There are left and right values for the variables in C + +, and their differences are mainly as follows:

(1) The left value can be placed on the left and right side of the assignment number =, and the rvalue can only be placed on the right of the assignment number =

(2) in C, a variable with a name is an lvalue, whereas a function's run result or an expression intermediate variable is a right-hand value.

(3) for the embedded type (basic type, that is, built-in types), the right value can not be changed , nor can be modified by const,volatile;

For a custom type, the right value can be modified by its member function.

(4) An lvalue can also be an rvalue expression , a variable can be an lvalue or an rvalue, but a constant can only be a right-hand value.

(5) The right value can only be pointed to by a reference to a const type, while an lvalue may be pointed to by a const or non-const type reference

In C + +, each expression produces an lvalue, or rvalue, and, accordingly, the expression is called an lvalue expression and an rvalue expression.

For example:

++a to add a, and then return a,a itself has a memory address, for an lvalue, so (++a) + + is correct;

And a++ is after self-increment, the value in the expression is still the original A, return a, and then A is assigned to the value of a+1. The returned value is a temporary variable that is an rvalue, cannot be changed, and does not have a memory address. So (a++) + + error.

An lvalue can only be a variable, not a constant, because a constant cannot be placed to the left of an assignment number, but an rvalue can be a variable or a constant, because both constants and variables can be placed to the right of the assignment number.

#include <iostream>using namespace Std;template<typename t>class referencewrapper{public:explicit Referencewrapper (t& T): T_ (&t) {};operator t& () Const{return *t_;} t& Get () Const{return *t_;} t* getpointer () Const{return t_;} void Print () {cout << *t_ << Endl;} Private:t* T_;}; Template<typename t>referencewrapper<t> getrw () {static T t = 0;return referencewrapper<t> (t++);} int Func () {static int a = 0;return a++;} int main () {int value = 10; Referencewrapper<int> rw (value);//constructor cout << rw + << endl;int value1 = + VALUE;RW = REFERENCEWR Apper<int> (value1);//assignment number, construct value = rw;//call t& operator () for implicit conversion, convert object to int type RW. Get () = value;//returns T&, can be assigned to Operation *RW. Getpointer () = value;//returns t*, and then assigns (int&) RW = value;//Converts the RW implicitly to t& the right value; required (int&) display conversion to perform assignment//RW = value ;//error, t& operator () function for implicit conversion, only as an rvalue, not as an lvalue//function to return a variable of a custom type, right value//But for a custom type, the right value can call the class's member function, that is, operator =, you can put in = Left getrw<int> () = RefeRencewrapper<int> (value1);//The function returns a variable of the inline type, which is the right value. cannot be placed on = left func () = 100;//The right value calls the member function inside the class getrw<int> (). Print (); The//func function returns a temporary variable that can call a member function for Rvalue/rvalue, and can only be pointed to by a reference of the const type. const int & tmp1 = func (); int & tmp2 = func ();//The lvalue can be either const or a reference to a non-const type pointing to int a = FUNC (); const int& Tmp3 = a;int& tmp4 = A;return 0;}

C + + left and right values

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.