设为首页 加入收藏

TOP

2.3.1 What Is a Variable?
2013-10-07 15:23:24 来源: 作者: 【 】 浏览:78
Tags:2.3.1 What Variable

A variable provides us with named storage that our programs can manipulate. Each variable in C++(www.cppentry.com) has a specific type, which determines the size and layout of the variable’s memory; the range of values that can be stored within that memory; and the set of operations that can be applied to the variable. C++(www.cppentry.com) programmers tend to refer to variables as “variables” or as “objects” interchangeably.

C++(www.cppentry.com) 中的“变量”就是object with a name,即带名字的对象。C++(www.cppentry.com) 的“对象”在下一页有定义。

Lvalues and Rvalues

We’ll have more to say about expressions in Chapter 5, but for now it is useful to know that there are two kinds of expressions in C++(www.cppentry.com):

1. lvalue (pronounced “ell-value”): An expression that is an lvalue may appear as either the left-hand or right-hand side of an assignment.

2. rvalue (pronounced “are-value”): An expression that is an rvalue may appear on the right- but not left-hand side of an assignment.

左值和右值的主要区别在于左值可以被赋值。当然,我们将会看到(见2.4节), 也有一些左值不能被赋值,称为unmodifiblelvalue 。左值(lvalue)中的字母l 的另外一个意思是“位置(location)”,即左值一定在内存里有位置,可以取其地址(用一元& 操作符),而右值不能取其地址,详见英文原版书第115 页。

Variables are lvalues and somay appear on the left-hand side of an assignment. Numeric literals are rvalues and so may not be assigned. Given the variables:

  1. int units_sold = 0;  
  2. double sales_price = 0, total_revenue = 0; 

it is a compile-time error to write either of the following:

  1. // error: arithmetic expression is not an lvalue  
  2. units_sold * sales_price = total_revenue;  
  3. // error: literal constant is not an lvalue  
  4. 0 = 1; 

Some operators, such as assignment, require that one of their operands be an lvalue. As a result, lvalues can be used in more contexts than can rvalues. The context in which an lvalue appears determines how it is used. For example, in the expression

  1. units_sold = units_sold + 1; 

the variable units_sold is used as the operand to two different operators. The + operator cares only about the values of its operands. The value of a variable is the value currently stored in the memory associated with that variable. The effect of the addition is to fetch that value and add one to it.

The variable units_sold is also used as the left-hand side of the = operator. The = operator reads its right-hand side and writes to its left-hand side. In this expression, the result of the addition is stored in the storage associated with units_sold; the previous value in units_sold is overwritten.  

In the course of the text, we’ll see a number of situations in which the use of an rvalue or lvalue impacts the behavior and/or the performance of our programs—in particular when passing and returning

EXERCISES SECTION 2.3.1

Exercise 2.12: Distinguish between an lvalue and an rvalue; show examples of each.

Exercise 2.13: Name one case where an lvalue is required.

TERMINOLOGY: WHAT IS AN OBJECT

C++(www.cppentry.com) programmers tend to be cavalier in their use of the term object. Most generally, an object is a region of memory that has a type. More specifically, eva luating an expression that is an lvalue yields an object.

C++(www.cppentry.com) 中的对象不一定是指“面向对象程序设计”中的对象,int、double、char*类型的变量都是对象;不过,函数不是对象(函数
对象/function object 是另外一个意思)。

Strictly speaking, some might reserve the term object to describe only variables or values of class types. Othersmight distinguish between named and unnamed objects, always referring to variables when discussing named objects. Still others distinguish between objects and values, using the term object for data that can be changed by the program and using the term value for those that are read-only.

In this book, we’ll follow the more colloquial usage that an object is a region of memory that has a type. We will freely use object to refer to most of the data manipulated by our programs regardless of whether those data have built-in or class type, are named or unnamed, or are data that can be read or written.

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇2.3.6 Scope of a Name 下一篇2.1.2 Floating-Point Types

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Sphinx : 高性能SQL (2025-12-24 10:18:11)
·Pandas 性能优化 - (2025-12-24 10:18:08)
·MySQL 索引 - 菜鸟教 (2025-12-24 10:18:06)
·Shell 基本运算符 - (2025-12-24 09:52:56)
·Shell 函数 | 菜鸟教 (2025-12-24 09:52:54)