Available CategoriesAdobeMacromediaProgrammingSQLServer AdministrationNetworkingMicrosoft ProductsMac OSLinux systemsMobile devicesXMLCertificationMiscAvailable Tutorials.NET Framework Essentials.NET Programming securityC# In A Nutshell TutorialProgramming C.SharpMastering Visual Studio .NETASP.NETWeb Solutions based on ASP.NET and ADO.NETJava data objectsJava extreme programmingJava performance tuningJava development on pda's. Building applications for pocket pc and palm devicesJavaScript and DHTMLLearning UMLUMLLearning XMLCocoaProgramming CppPerl objects, references and modulesPerl tutorialPython tutorialPython. Text processingPocket pc network programmingPHP & MySQL. Building web database applicationsPHP & MySQL. Programming for beginnersPHP, MySQL and Apache in 24 hoursSoftware architecture in practiceSoftware engineering and computer gamesBuilding Solutions With the Microsoft .NET Compact FrameworkProgramming Microsoft Visual C# 2005ActionscriptMastering Delphi 7Ado.netPractical mod_perlPerl for bioinformaticsWeb ServicesPrinciples of Secure CodingC/C++ Secure ProgrammingASP.NET AJAXVisual C#Borland C++ Builder 6 Developer's Guide |
3.1 Lvalues and Rvalues
An lvalue is an expression that yields an object reference, such as a variable name, an array subscript reference, a dereferenced pointer, or a function call that returns a reference. An lvalue always has a defined region of storage, so you can take its address. An rvalue is an expression that is not an lvalue. Examples of rvalues include literals, the results of most operators, and function calls that return nonreferences. An rvalue does not necessarily have any storage associated with it. Strictly speaking, a function is an lvalue, but the only uses for it are to use it in calling the function, or determining the function's address. Most of the time, the term lvalue means object lvalue, and this book follows that convention. C++ borrows the term lvalue from C, where only an lvalue can be used on the left side of an assignment statement. The term rvalue is a logical counterpart for an expression that can be used only on the righthand side of an assignment. For example: #define rvalue 42 int lvalue; lvalue = rvalue; In C++, these simple rules are no longer true, but the names remain because they are close to the truth. The most significant departure from traditional C is that an lvalue in C++ might be const, in which case it cannot be the target of an assignment. (C has since evolved and now has const lvalues.) The built-in assignment
operators require an lvalue as
their lefthand operand. The built-in address
(&) operator also requires an lvalue operand,
as do the increment (++) and decrement
(--) operators. Other operators require rvalues.
The rules are not as strict for user-defined operators. Any object,
including an rvalue, can be used to call member functions, including
overloaded
Some other rules for lvalues and rvalues are:
Example 3-1 shows several different kinds of lvalues and rvalues. Example 3-1. Lvalues and rvaluesclass number {
public:
number(int i = 0) : value(i) {}
operator int( ) const { return value; }
number& operator=(const number& n);
private:
int value;
};
number operator+(const number& x, const number& y);
int main( )
{
number a[10], b(42);
number* p;
a; // lvalue
a[0]; // lvalue
&a[0]; // rvalue
*a; // lvalue
p; // lvalue
*p; // lvalue
10; // rvalue
number(10); // rvalue
a[0] + b; // rvalue
b = a[0]; // lvalue
}
|