在C#和C ++我部经营差异++ [英] ++i operator difference in C# and C++

查看:123
本文介绍了在C#和C ++我部经营差异++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经写在C ++和C#下面的代码

I have the following code written in both C++ and C#

 int i=0;
 ++i = 11;

在此C#编译器带来的误差

After this C# compiler brings an error

The left-hand side of an assignment must be a variable, property or indexer

不过,C ++编译器生成的代码没有错误,我得到的结果 11 的值i 。这是什么差异的原因是什么?

But C++ compiler generate this code with no error and I got a result 11 for value of i. What's the reason of this difference?

推荐答案

不同的是,预先递增运算符为左值在C ++和是不是在C#。结果
在C ++ ++我返回递增变量的引用。在C# ++我返回的变量i。结果
增加后的值因此,在这种情况下, ++我左值在C ++和右值在C#。

The difference is that pre-increment operator is lvalue in C++, and isn't in C#.
In C++ ++i returns a reference to the incremented variable. In C# ++i returns the incremented value of variable i.
So in this case ++i is lvalue in C++ and rvalue in C#.

从C ++规范有关前缀增量运营商

From C++ specification about prefix increment operator

类型的操作数应该是一个算术类型或指向一个
完全定义的对象类型。的值是
操作数的新值;它是一个左值

The type of the operand shall be an arithmetic type or a pointer to a completely-defined object type. The value is the new value of the operand; it is an lvalue.

PS 后缀增量运算符我++是不是。左值在C#和C ++,所以这个代码行会在两种语言带来误差

P.S. postfix increment operator i++ isn't lvalue in both C# and C++, so this lines of code will bring error in both languages.

 int i=0;
 i++ = 11;

这篇关于在C#和C ++我部经营差异++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆