C++ C++;运算符优先查询

C++ C++;运算符优先查询,c++,operator-keyword,C++,Operator Keyword,网站上说 前缀++和*的优先级相同。两者的关联性是从右向左的 后缀++的优先级高于*和前缀++。postfix++的关联性是从左到右的 第一个代码示例: int x[4] = {0, 1, 2, 3}; int *ptr = x; cout << x[0] << " at " << &x[0] << "\n"; cout << x[1] << " at "

网站上说

前缀++和*的优先级相同。两者的关联性是从右向左的

后缀++的优先级高于*和前缀++。postfix++的关联性是从左到右的

第一个代码示例:

int x[4] = {0, 1, 2, 3};
int *ptr = x;

cout << x[0] << " at " << &x[0] << "\n";
cout << x[1] << " at " << &x[1] << "\n";
cout << x[2] << " at " << &x[2] << "\n";
cout << x[3] << " at " << &x[3] << "\n";
cout << "*ptr = " << *ptr << " at " << ptr << "\n";
cout << "*++ptr = " << *++ptr << " at " << ptr << "\n";
cout << "++*ptr = " << ++*ptr << " at " << ptr << "\n";
cout << "*ptr++ = " << *ptr++ << " at " << ptr << "\n";
int cd = 7;
cout << "cd = " << cd << " at " << &cd<< "\n";
cout << "++cd = " << ++cd << " at " << &cd << "\n";
cout << "cd++ = " << cd++ << " at " << &cd << "\n";
int c = 10;
int d = 1;
cout << c << " at " << &c << "\n";
int e = c+++d;
cout << c << " at " << &c << "\n";
cout << d << " at " << &d << "\n";
cout << e << " at " << &e << "\n";
在最后一个cout语句中,除了增量后指针,它在使用之前首先递增指针“ptr”值

第二个代码示例:

int x[4] = {0, 1, 2, 3};
int *ptr = x;

cout << x[0] << " at " << &x[0] << "\n";
cout << x[1] << " at " << &x[1] << "\n";
cout << x[2] << " at " << &x[2] << "\n";
cout << x[3] << " at " << &x[3] << "\n";
cout << "*ptr = " << *ptr << " at " << ptr << "\n";
cout << "*++ptr = " << *++ptr << " at " << ptr << "\n";
cout << "++*ptr = " << ++*ptr << " at " << ptr << "\n";
cout << "*ptr++ = " << *ptr++ << " at " << ptr << "\n";
int cd = 7;
cout << "cd = " << cd << " at " << &cd<< "\n";
cout << "++cd = " << ++cd << " at " << &cd << "\n";
cout << "cd++ = " << cd++ << " at " << &cd << "\n";
int c = 10;
int d = 1;
cout << c << " at " << &c << "\n";
int e = c+++d;
cout << c << " at " << &c << "\n";
cout << d << " at " << &d << "\n";
cout << e << " at " << &e << "\n";
在这里的最后一个cout语句中,请注意cd递增,然后使用post increment操作符进行访问

第三个代码示例:

int x[4] = {0, 1, 2, 3};
int *ptr = x;

cout << x[0] << " at " << &x[0] << "\n";
cout << x[1] << " at " << &x[1] << "\n";
cout << x[2] << " at " << &x[2] << "\n";
cout << x[3] << " at " << &x[3] << "\n";
cout << "*ptr = " << *ptr << " at " << ptr << "\n";
cout << "*++ptr = " << *++ptr << " at " << ptr << "\n";
cout << "++*ptr = " << ++*ptr << " at " << ptr << "\n";
cout << "*ptr++ = " << *ptr++ << " at " << ptr << "\n";
int cd = 7;
cout << "cd = " << cd << " at " << &cd<< "\n";
cout << "++cd = " << ++cd << " at " << &cd << "\n";
cout << "cd++ = " << cd++ << " at " << &cd << "\n";
int c = 10;
int d = 1;
cout << c << " at " << &c << "\n";
int e = c+++d;
cout << c << " at " << &c << "\n";
cout << d << " at " << &d << "\n";
cout << e << " at " << &e << "\n";
我们看到,++在语句中访问vars值后,会增加vars值

问题是第三个代码示例与前两个代码示例不同,为什么增量后运算符在访问变量“c”之前不增加它的值? 为什么在上一个代码示例中,变量没有收到12的值

“为什么增量后运算符不增加变量的值 “c”在访问它之前

因为它是一个后增量操作符。它计算其操作数,然后递增

所以
inte=c+++d被解释为“将
c
+
d
分配给
e
,并增加
c


在前两个例子中,你编写了一个代码,它改变一个变量的值,并对该变量进行评估,没有任何序列点来限制操作的顺序。

是<代码> CUT @弗兰Cou-OISANDURIX(C++(?):)的版本]Yakk AdamNevraumont为什么以及如何改变?看起来它不再是UB了,因为c++17是可信的。我想我必须重新学习这一切。编辑:虽然我找不到什么改变了。后续:谢谢蒂姆的回复。这是我们所期望的。但在前两个代码示例中,情况并非如此。为什么在前两个示例代码中它首先递增并计算操作数?谢谢。我补充了答案