c++ - x++ 和++x 在c 中不能作为左值(左值)?

标签 c++ c

偶尔,我被问到一个技巧问题:x++++x 哪个不能在 c 中留值?很多人告诉我++x不能,因为++x的汇编代码不返回寄存器。我对此表示怀疑。所以我做了一些实验。

C 代码:

#include <stdio.h>

int main()
{
    int a = 1;
    a++ = 10; 
    ++a = 10; 
    return a;
}

gcc 编译错误:

./test.c:6: invalid lvalue in assignment
./test.c:7: invalid lvalue in assignment

更改代码和程序集:

int a = 1;
int b = a++; 
int c = ++a; 

汇编代码:

0x08048400 <main+0>:    push   %ebp
0x08048401 <main+1>:    mov    %esp,%ebp
0x08048403 <main+3>:    sub    $0x18,%esp
0x08048406 <main+6>:    movl   $0x1,-0x4(%ebp)
0x0804840d <main+13>:   mov    -0x4(%ebp),%eax
0x08048410 <main+16>:   mov    %eax,-0x8(%ebp)
0x08048413 <main+19>:   incl   -0x4(%ebp)
0x08048416 <main+22>:   incl   -0x4(%ebp)
0x08048419 <main+25>:   mov    -0x4(%ebp),%eax
0x0804841c <main+28>:   mov    %eax,-0xc(%ebp)
0x0804841f <main+31>:   mov    -0x4(%ebp),%edx
0x08048422 <main+34>:   mov    %edx,%eax
0x08048424 <main+36>:   jmp    0x8048426 <main+38>
0x08048426 <main+38>:   mov    %ebp,%esp
0x08048428 <main+40>:   pop    %ebp
0x08048429 <main+41>:   ret

看来++x和x++只是语义上的区别。为什么很多人说++a可以是左值,而a++却不能呢?

有什么问题我没弄明白吗?或者他们只是胡说八道?

读完 Keith Thompson 的回答后,我猜在 c++ 中,当 x 是用户定义类型时 x++ 导致后缀运算符++ 的实现(它返回一个临时对象),所以 x++ 不能是左值。

#include <iostream>
using namespace std;

class my_test
{
        int a;

public:
        void printf()
        {   
                cout << a << endl;
        }   


        my_test(int c)
        {   
                a = c;
        }    


        my_test& operator++()
        {   
                ++a;
                return *this;
        }   


        my_test& operator++(int)
        {   
                my_test temp(a);
                ++a;
                return temp;
        }   
};



int main(int argc, char **argv)
{
        int a = 10; 
        int b = ++a;
        int c = a++;

        my_test obj1(1);
        my_test obj2(2);

        obj1++ = obj2;
        ++obj1 = obj2;

        obj1.printf();
        obj2.printf();

        return 0;
}

我用g++(version:g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-17))编译代码,只是得到一个警告信息。

[root@localhost ~]# g++ -g3 ./1.cpp -O0
./1.cpp: In member function 'my_test& my_test::operator++(int)':
./1.cpp:30: warning: reference to local variable 'temp' returned
[root@localhost ~]# 

似乎在c++中,++x和x++都可以是左值?

最佳答案

术语是左值,而不是“左值”。

在 C 中,x++++x 都不是左值。这是因为语言是这样定义的;它与汇编代码没有特别的关系。

(在 C++ 中,++x 是左值而 x++ 不是。可能是您混淆的根源。)

(您已更新您的问题以询问 C++ 中重载的 ++ 运算符。我已经回答了原始问题。如果您有关于具有不同规则的不同语言的新问题,请发帖它分开。)

关于c++ - x++ 和++x 在c 中不能作为左值(左值)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43036807/

相关文章:

c - while ( ) 循环中的 "--"运算符

我能否确保某个线程始终在没有信号量的情况下最后工作?

c - 库名和函数名混淆

c - 不一致的严格别名规则

c++ - 无法使用对象访问迭代器数据成员

c++ - 在 C++ 中模仿类似 Golang 的接口(interface)

c++ - ADO Jet SQL 总是抛出 "Syntax Error in FROM Clause"

c++ - Clang 模板不完整类型

c++ - 检查输入值是否为 float ,清除 cin

c - 如何使用递归打印两个数组,一个接一个(即不同时)?