我在cppreference上遇到了评估顺序页面,我无法理解为什么这些行被视为未定义行为。
i = ++i + 2; // undefined behavior until C++11
f(i = -2, i = -2); // undefined behavior until C++17
Run Code Online (Sandbox Code Playgroud)
在第一种情况下,不是先++i计算赋值,然后是求和,然后是最终赋值i?
第二种情况呢?在我看来,该函数将同时获得参数as -2和的值i将始终-2在执行之后。
可能重复:
C#:功能评估顺序(vs C)
代码段:
i += ++i;
a[++i] = i;
int result = fun() - gun();
//statement of similar kind
Run Code Online (Sandbox Code Playgroud)
他们的行为在C#中定义得很好吗?在C++中,此类代码调用未定义/未指定的行为.还请在回复中引用语言规范中的相关部分!
当我使用i++++给编译错误:
for (int i=1;i<=10;i++++) {} //a.cpp:63: error: lvalue required as increment operand
Run Code Online (Sandbox Code Playgroud)
要么
int i = 0;
i++++; // a.cpp:65: error: lvalue required as increment operand
Run Code Online (Sandbox Code Playgroud)
但是当我使用的时候++++i.有人解释我为什么++++i经常但i++++不规律?
谢谢.
我被要求在不使用临时变量或使用xor的情况下编写交换,我想出了这个.
在Java中,这可行,但在C/C++中,这不起作用.
我的印象是,由于'|'左侧'a'的值,这总是有效的 将被存储在寄存器中,然后分配给'a'将否定对'b'的指定值的影响.
int a = 5;
int b = -13;
b = a | (0 & (a = b));
Run Code Online (Sandbox Code Playgroud) 下面有一个代码,我在理解代码的逻辑时遇到了一个非常严重的问题.
#include <stdio.h>
#include <stdlib.h>
int main(void )
{
int i = 1 ;
printf("\n%d %d %d %d\n",++i,i++,i++,++i) ;
return 0 ;
}
Run Code Online (Sandbox Code Playgroud)
我在linux发行版下使用名为Mandriva的gcc编译器.在上面提到的我在printf语句中使用了一个变量的pre和post增量.我应该得到的输出是2 2 3 5,但我得到一个不同的输出.请帮我这个代码.
我觉得这段代码很难.
我有一个自定义类MyInt,封装了一个int m_nValue数据.后缀运算符
MyInt operator++(int)
{
MyInt temp(*this);
++(*this);
return temp;
}
Run Code Online (Sandbox Code Playgroud)
如果运算符返回一个对象,那么为什么我不能多次调用postfix ++运算符,如:
MyInt value(10);
value++; // valid
value++++; // Why is this invalid?? Cant it be interpreted as (value++)++
Run Code Online (Sandbox Code Playgroud)
如果我可以调用MyInt类中定义的value.print()方法,为什么值++++会给出一个错误左值,那么我还应该能够做值+++?
可能重复:
任何人都可以解释这些未定义的行为(i = i ++ + ++ i,i = i ++等...)
Java和C++运算符之间有什么区别吗?
为什么一元运算符在c ++和java中给出不同的结果?
看一下这个:
int i = 1;
i = i++ + ++i;
print i (with cout or println)
Run Code Online (Sandbox Code Playgroud)
在java中:打印4
在c ++中:打印5
为什么?
int ASCI1 = 1;
for (int i = 1; i < 8; i++)
{
cout << ASCI1 << endl << ASCI1++;
}
Run Code Online (Sandbox Code Playgroud)
我得到输出:
2
13
24
35
46
57
68
Run Code Online (Sandbox Code Playgroud)
7 (由于某种原因,这里没有终点线).
这个想法是为了得到
1
2
3
4
etc.
Run Code Online (Sandbox Code Playgroud)
完全难倒,请帮忙!
我遇到了以下程序:
class Counter {
protected:
unsigned int count;
public:
Counter(): count(0) {}
Counter(int c): count(c) {}
unsigned int get_count() { return count; }
Counter operator++() { return Counter(++count); }
};
Run Code Online (Sandbox Code Playgroud)
最后一个成员函数做什么(Counter(++count))?
我总是希望它的工作原理如下所示:"x = x ++"后的x是什么?
但是当我试图测试它时:
int x = 0;
x = x++;
printf("x = %d\n", x);
Run Code Online (Sandbox Code Playgroud)
结果不是我想象的那样但是1.我们在VS2012和g ++(版本4.7)中测试了它.
请注意,此代码按预期打印0:
int x = 0;
int y = x++;
printf("y = %d\n", y);
Run Code Online (Sandbox Code Playgroud) c++ ×8
java ×2
c ×1
c# ×1
expression ×1
inline ×1
operators ×1
puzzle ×1
swap ×1
visual-c++ ×1