gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 05:23:48 25 4
gpt4 key购买 nike

偶尔,我被问到一个技巧问题: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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com