【发布时间】:2020-11-18 16:20:31
【问题描述】:
我很困惑。我希望函数中的第一个打印语句会打印 6 而不是 5。因为 a++ + c++ = (1+1) + (3+1) = (2+4 = 6)。
#include <iostream>
#include <string>
int main()
{
int a = 1;
int b = 2;
std::cout << "intitialisation b:"<< b << std::endl;
int c = 3;
{
auto b = ++a + c++;
std::cout << "increment b in function:" << b << std::endl;
auto e = b;
c += ++b;
std::cout << "increment b in function:" << b << std::endl;
}
std::cout << "increment b out function:" << b << std::endl;
int* p = &a;
int* q = &b;
std::cout <<"value pointer:" << *q << std::endl;
++(*q);
std::cout <<"value pointer:" << *q << std::endl;
*p += a++ + (*p)++;
}
intitialisation b:2
increment b in function:5
increment b in function:6
increment b out function:2
value pointer:2
value pointer:3
纳丁
【问题讨论】:
-
++a递增后返回值。c++在递增之前返回值。 -
另外,您的代码和问题文本并不真正匹配。
-
the first print statementfirst 打印intitialisation b:2 -
您之前发布了相同的代码,我们已经知道
*p += a++ + (*p)++;没有好处。如果您的问题只是关于auto b = ++a + c++;,请考虑将您的代码减少到minimal reproducible example。您已经得到一个引用整个代码的答案,但我想如果您更改代码,回答者不会太不高兴
标签: c++