• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            程序讓生活更美好

            半畝方塘 天光云影

              C++博客 ::  :: 新隨筆 :: 聯(lián)系 ::  :: 管理 ::
              55 隨筆 :: 4 文章 :: 202 評論 :: 0 Trackbacks

            左值和右值

            我們在看書的時候,經(jīng)常可以看到關(guān)于 左值(L-value) 和 右值(R-value) 的概念,那么到底什么是左值,什么是右值,它們之間的區(qū)別又是在哪里呢?

                通俗的講,左值就是能夠出現(xiàn)在賦值符號左面的東西,而右值就是那些可以出現(xiàn)在賦值符號右面的東西了。

            舉個很簡單的例子:

            a=b+100;

            那么這里a就是左值,b+25就是一個右值。左值和右值之間是不一定都能互換的,上面的這個例子就是不能互換的,如果寫成

            b+100=a;

            大家都能看出來這樣寫會不編譯通過的,因為編譯器無法判斷b+100的內(nèi)存地址,所以不能操作。

            看了這個例子,可以做一個總結(jié),左值必須應(yīng)該是一個變量或者是表達(dá)式等,但是它的物理位置是可以確定的,而右值不一定,這也是它們兩者之間的區(qū)別。

            關(guān)于左值是表達(dá)式的例子有數(shù)組,還有指針這些都可以。

            int array[10];

            int a=5;

            array[a+3]=10; //這里左值就是一個數(shù)組表達(dá)式了

             

            此文完。

             

             另外:liotta朋友給了一些其他方面的提示 (左值右值翻譯可能有些問題)

            錯了,沒有什么左值和右值!
            翻譯害人不淺,
            L-value中的L指的是Location,表示可尋址。The "l" in lvalue can be though of as location
            R-value中的R指的是Read,表示可讀。The "r" in rvalue can be thought of as "read" value.   
            posted on 2006-05-25 10:30 北風(fēng)之神007 閱讀(18185) 評論(24)  編輯 收藏 引用 所屬分類: c/c++

            評論

            # re: 左值和右值 2007-09-07 08:51 wu
            那么對于(a=2)=66這樣的表達(dá)式怎么看?另外,為什么自增(減)運(yùn)算只能用于左值,不能用于右值,即表達(dá)式++(a++)為什么錯誤呢?  回復(fù)  更多評論
              

            # re: 左值和右值 2007-09-07 17:27 JetSun
            @wu
            (a=2)=66 中就是按順序賦值就是了 所以a=66

            ++(a++)
            至于右值為什么不能用,因為后增量(后減量)返回的值是修改前的變量值,故不為左值,所以他就不能用了  回復(fù)  更多評論
              

            # re: 左值和右值 2008-06-07 19:28 Nash
            ++(a++)
            a++相當(dāng)于
            int a;
            {
            int temp=a;
            a++;
            teturn temp;
            }
            所以我們可以將++(a++)看成++temp;而temp
            顯然是一個右值,所以不能用啊~~  回復(fù)  更多評論
              

            # re: 左值和右值 2008-09-01 18:29 liotta
            錯了,沒有什么左值和右值!
            翻譯害人不淺,
            L-value中的L指的是Location,表示可尋址。The "l" in lvalue can be though of as location
            R-value中的R指的是Read,表示可讀。The "r" in rvalue can be thought of as "read" value.  回復(fù)  更多評論
              

            # re: 左值和右值 2008-09-23 15:59 xautfengzi
            原創(chuàng)好文章,謝謝樓主。  回復(fù)  更多評論
              

            # re: 左值和右值[未登錄] 2009-02-14 07:25 Richard
            From Wikipedia, the free encyclopedia
            Jump to: navigation, search
            L-value or L value may refer to:

            A value (computer science) that has an address
            The value assigned to an L-shell, a particular set of planetary magnetic field lines
            A measure of brightness of a lunar eclipse on the Danjon scale

            From Wikipedia, the free encyclopedia
            Jump to: navigation, search
            R-value can refer to:

            Properties of materials:
            R-value (insulation), the efficiency of insulation
            R-value (soils), stability of soils and aggregates for pavement construction
            r-value, in computer science, a value that does not have an address in a computer language
            R-factor (crystallography), a measure of the agreement between the crystallographic model and the diffraction data
            In statistics, r (or r value) refers to the Pearson product-moment correlation coefficient, often called simply correlation coefficient

            翻譯不對,謝謝  回復(fù)  更多評論
              

            # re: 左值和右值 2009-02-20 10:37 單曲旋律
            這是<C++ primmer>中的一部分:

            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++:

            我們在第五章再詳細(xì)探討表達(dá)式,現(xiàn)在先介紹 C++ 的兩種表達(dá)式:

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

            左值(發(fā)音為 ell-value):左值可以出現(xiàn)在賦值語句的左邊或右邊。

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

            右值(發(fā)音為 are-value):右值只能出現(xiàn)在賦值的右邊,不能出現(xiàn)在賦值語句的左邊。

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

            變量是左值,因此可以出現(xiàn)在賦值語句的左邊。數(shù)字字面值是右值,因此不能被賦值。給定以下變量:

            int units_sold = 0;
            double sales_price = 0, total_revenue = 0;



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

            下列兩條語句都會產(chǎn)生編譯錯誤:

            // error: arithmetic expression is not an lvalue
            units_sold * sales_price = total_revenue;
            // error: literal constant is not an lvalue
            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

            有些操作符,比如賦值,要求其中的一個操作數(shù)必須是左值。結(jié)果,可以使用左值的上下文比右值更廣。左值出現(xiàn)的上下文決定了左值是如何使用的。例如,表達(dá)式

            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.

            中,units_sold 變量被用作兩種不同操作符的操作數(shù)。+ 操作符僅關(guān)心其操作數(shù)的值。變量的值是當(dāng)前存儲在和該變量相關(guān)聯(lián)的內(nèi)存中的值。加法操作符的作用是取得變量的值并加 1。

            The variable units_sold is also used as the left-hand side of

            可以看出,左值和右值是跟左和右掛鉤的。  回復(fù)  更多評論
              

            # re: 左值和右值 2009-09-16 21:12 whtank
            ++(a++)
            a++ return a const object we can say it is b. so ++b is wrong  回復(fù)  更多評論
              

            # re: 左值和右值 2010-01-05 00:32 似的
            @whtank
            高度啊 頂  回復(fù)  更多評論
              

            # re: 左值和右值 2010-04-13 14:22 溪流
            l, r 的原始含義真的這樣啊?剛才同事跟我說我還不信呢。。。  回復(fù)  更多評論
              

            # re: 左值和右值 2010-05-27 16:19 wiskey
            msdn上的,左值這種翻譯并無差錯。

            An l-value represents a storage region's "locator" value, or a "left" value, implying that it can appear on the left of the equal sign (=). L-values are often identifiers.  回復(fù)  更多評論
              

            # re: 左值和右值 2010-07-21 21:42 rslonghorn
            我認(rèn)為左值右值才是正確的  回復(fù)  更多評論
              

            # re: 左值和右值 2010-07-22 17:52 nashtty
            感謝樓主。。l-value r-value 真神秘  回復(fù)  更多評論
              

            # re: 左值和右值 2010-08-08 17:04 jackhao168
            Expressions that refer to memory locations are called "l-value" expressions. An l-value represents a storage region's "locator" value, or a "left" value, implying that it can appear on the left of the equal sign (=). L-values are often identifiers.

            The term "r-value" is sometimes used to describe the value of an expression and to distinguish it from an l-value. All l-values are r-values but not all r-values are l-values.

              回復(fù)  更多評論
              

            # re: 左值和右值 2010-08-08 17:08 jackhao168
            應(yīng)該是先有了l-value這個概念,r-value只是起到與lvalue對比的作用。l-value含有左的意思,但不僅僅是左的意思,r-value只有右的意思  回復(fù)  更多評論
              

            # re: 左值和右值 2010-12-20 10:42 LuckyGeb
            @wu
            不對吧,我記得賦值作為副產(chǎn)品的返回是R值,不是地址,你的第一個表達(dá)式就不對啊  回復(fù)  更多評論
              

            # re: 左值和右值 2011-08-12 16:19 KevinKitty
            感謝樓主的分享。

            關(guān)于左值和右值,Dan Saks發(fā)表過一篇題為Lvalues and Rvalues的文章,閱讀之感覺寫的很好,概念一下清晰很多,順便翻譯了一下。有興趣的朋友可以交流學(xué)習(xí)一下。
            譯文:http://filemarkets.com/file/kevinkitty/0575b639/
            原文:http://www.eetimes.com/discussion/programming-pointers/4023341/Lvalues-and-Rvalues  回復(fù)  更多評論
              

            # re: 左值和右值 2011-08-16 12:07 KevinKitty
            @KevinKitty

            不好意思,譯文鏈接給錯了,應(yīng)該是:http://filemarkets.com/file/kevinkitty/03c933d9/  回復(fù)  更多評論
              

            # re: 左值和右值 2012-03-07 20:52 jie
            @Nash
            a++相當(dāng)于
            int a;
            {
            int temp=a;
            a++;
            teturn temp;
            }

            用a++,解釋a++.呵呵,太有才了
            寫成a=a+1.更好理解吧  回復(fù)  更多評論
              

            # re: 左值和右值 2012-03-09 12:32 JetSun
            謝謝討論 這樣百家爭鳴 挺好@jie
              回復(fù)  更多評論
              

            # re: 左值和右值[未登錄] 2012-03-22 11:20 LEO
            看了樓上給的原文,感覺判斷l(xiāng)value和rvalue的方法就是看if it is refer to an object。因為原文中有這樣一句An object is a manipulatable region of storage; an lvalue is an expression referring to an object....  回復(fù)  更多評論
              

            # re: 左值和右值 2012-06-27 14:16 給力電
            其實我感覺左值和右值的概念吧,可能跟匯編有關(guān)。

            比如 MOV A, #300

            其中A就必須是物理地址,或者累加器  回復(fù)  更多評論
              

            # re: 左值和右值[未登錄] 2013-03-14 14:09 1
            locator  回復(fù)  更多評論
              

            # re: 左值和右值 2016-05-09 16:10 hj
            @wu
            因為自增和自減運(yùn)算符是有副作用的  回復(fù)  更多評論
              

            一本色道久久综合狠狠躁| 国产精品久久久久jk制服| 国内精品伊人久久久久影院对白| 国产一区二区三区久久| 精品久久久无码中文字幕| 亚洲愉拍99热成人精品热久久 | 99999久久久久久亚洲| 亚洲欧美精品伊人久久| 日韩欧美亚洲综合久久影院Ds | 久久精品国产99国产精偷| 日本福利片国产午夜久久| 久久久久久久波多野结衣高潮| 国产精品久久永久免费| 久久精品无码一区二区三区日韩 | 精品999久久久久久中文字幕| 亚洲国产成人精品女人久久久 | 久久久久18| 91精品观看91久久久久久 | 亚洲七七久久精品中文国产| 亚洲av成人无码久久精品| 99久久国产主播综合精品 | 久久中文字幕人妻熟av女| 性欧美丰满熟妇XXXX性久久久 | 久久艹国产| 99久久99久久| 91精品日韩人妻无码久久不卡| 97久久国产综合精品女不卡 | 国产日韩久久久精品影院首页| 欧美激情精品久久久久久久| 亚洲欧美日韩久久精品第一区| 大美女久久久久久j久久| 一97日本道伊人久久综合影院| 精品久久久久久无码专区不卡| 精品久久久久久久久久中文字幕 | 久久国产视频网| 男女久久久国产一区二区三区| 国产精品九九久久精品女同亚洲欧美日韩综合区| 久久99精品国产99久久| 久久人人爽人人爽人人片av麻烦| 91精品国产91久久久久久| 亚洲综合熟女久久久30p|