what is lvalue and rvalue references in c++?

1 Answer

Relevance
  • 6 months ago

    The easiest way to think of them as data types valid as the left and right values for an assignment.

    Example:

    int var1;

    int var2;

    var1 = var2;

    // var1 is a suitable lvalue, var2 is a suitable rvalue.

    3 = var2; // Invalid code - a numeric constant cannot be an lvalue

    var1 = 3; // but a number can be an rvalue.

    An lvalue is something that can have a value assigned to it, a variable or pointer etc. for use later in the program or function etc..

    An rvalue is (or can be) a constant or result that does not exist outside the statement it is used in.

Still have questions? Get your answers by asking now.