请考虑以下代码
#include <iostream>
using namespace std;
class Digit
{
private:
int m_digit;
public:
Digit(int ndigit=0){
m_digit=ndigit;
}
Digit& operator++();//prefix
Digit& operator--(); //prefix
Digit operator++(int);
Digit operator--(int);
int get() const { return m_digit;}
};
Digit& Digit::operator++(){
++m_digit;
return *this;
}
Digit& Digit::operator--(){
--m_digit;
return *this;
}
Digit Digit::operator++(int){
Digit cresult(m_digit);
++(*this);
return cresult;
}
Digit Digit::operator--(int){
Digit cresult(m_digit);
--(*this);
return cresult;
}
int main(){
Digit cDigit(5);
++cDigit;
cDigit++;
cout<<cDigit.get()<<endl;
cout<<cDigit.get()<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这里实现了两个版本的后缀和前缀运算符,我已经读过,通过引入另一个所谓的伪参数来做出差异,但我有疑问,如果我们看到这些的声明
Digit& operator++();//prefix
Digit& operator--(); //prefix
Digit operator++(int); …Run Code Online (Sandbox Code Playgroud) 我正在讨论前缀增量运算符,我们似乎遇到了分歧.
运行此代码时:
var x = 0;
x = ++x;
Run Code Online (Sandbox Code Playgroud)
是第二行相当于:
很难区分,因为结果是相同的(都导致x值为1)
我相信当赋值的左侧是变量本身时,该值不会保存到原始变量中.
我的对手不同意并认为只要使用++操作员,该值就会保存到原始变量中.
我们哪一个是对的?
为什么这个代码示例在c ++和C#中表现不同.
[C++示例]
int arr[2];
int index = 0;
arr[index] = ++index;
Run Code Online (Sandbox Code Playgroud)
结果将是arr [1] = 1 ;
[C#示例]
int[] arr = new int[2];
int index = 0;
arr[index] = ++index;
Run Code Online (Sandbox Code Playgroud)
结果将是arr [0] = 1 ;
我觉得这很奇怪.当然,两种语言必须有不同的理由来实现它吗?我想知道C++/CLI会输出什么?
我在java中尝试一元postfix和前缀运算符
这是代码
int a=10;
Run Code Online (Sandbox Code Playgroud)
这行代码不会产生编译时错误
System.out.println(a+++ a +++a);
Run Code Online (Sandbox Code Playgroud)
但这条线确实如此
System.out.println(a++ +++a);
Run Code Online (Sandbox Code Playgroud)
而这条线甚至没有
System.out.println(a+++ ++a);
Run Code Online (Sandbox Code Playgroud)
我无法理解编译器如何解释这些查询的模式.
class compl{
float re,im;
public:
compl(float r, float i)
{re=r; im=i;}
compl& operator++()
{++re; return*this;} //(1)
compl operator++(int k){
compl z=*this; re++; im+=k; return z;} //(2)
friend compl& operator--(compl& z)
{--z.re; return z;}
friend compl operator--(compl& z,int k)
{compl x=z; z.re--; z.im-=k; return x;}
};
Run Code Online (Sandbox Code Playgroud)
(1)为什么我们必须通过引用返回当前对象?据我所知,引用只是某个东西的第二个名称.
(2)为什么我们必须在z中保存当前对象,然后更改对象并返回未更改的z?这样做,我们将返回未增加的值.是因为后缀运算符的工作方式(它返回旧的值,然后增加它)
是否有可能解析一个表达式(没有歧义),它可以包含二进制前缀,二进制中缀和二进制后缀运算符(让我们假设所有符号都不同),它们之间具有优先权?例如:
a = 2 3 post+
b = pre+ 2 3*4
Run Code Online (Sandbox Code Playgroud)
然后a将等于5因为=优先级低于后缀post+运算符并且b将是14.我知道你可以用运算符优先解析或分流码解析中缀表示法,但这个问题对我来说似乎要复杂得多.
编辑:
允许括号,并且运算符的前/后变体具有与中缀相同的优先级.
我想推出一个手写算法.
EDIT2:
按优先顺序,我指的是消费多少.例如:
a = 2 3 post+
Run Code Online (Sandbox Code Playgroud)
可能导致这些AST-s:
'=' has higher precedence than 'post+':
post+
/ \
= 3
/ \
a 2
'post+' has higher precedence than '=':
=
/ \
a post+
/ \
2 3
Run Code Online (Sandbox Code Playgroud)
(第二个是我在这种情况下需要的).我不能真正使用现有的解析器生成器或固定语法来操作数,因为运算符是动态加载的.
parsing operator-precedence infix-operator prefix-operator postfix-operator
我正在努力使¬成为一个逻辑否定运算符.
¬ True;
multi sub prefix:<¬> ($n) {
return not $n;
}
Run Code Online (Sandbox Code Playgroud)
当我运行上面的程序时,它返回此错误:
Run Code Online (Sandbox Code Playgroud)$ perl6 test.pl6 ===SORRY!=== Error while compiling /home/devXYZ/test.pl6 Bogus statement at /home/devXYZ/test.pl6:1 ------> <BOL>?¬ True; expecting any of: prefix term
有谁知道原因可能是什么?
但是,在优先级表中明确写出后缀运算符的优先级高于前缀.但我仍然有一个daubt.
我从以下示例开始:
*ptr++; // evaluate as *(ptr++);
++*ptr; // evaluate as ++(*ptr);
Run Code Online (Sandbox Code Playgroud)
这证明后缀运算符的优先级高于前一个.
现在在下面的示例中,它似乎不正确:
int a=0,b,c;
b=a++; //b=0 ,here it seems ++ has lower priority that is after assignment increment is performed.
c=++a; //b=2 ,here it seems ++ has higher priority.
Run Code Online (Sandbox Code Playgroud)
在上面的例子中,似乎postfix运算符的优先级低于前缀吗?
我错了,写道:
++number++;
Run Code Online (Sandbox Code Playgroud)
得到了这个:
Uncaught ReferenceError: Invalid left-hand side expression in prefix operation
Run Code Online (Sandbox Code Playgroud)
为什么?除了这个之外,我要先加number1然后再加number1.
javascript compiler-errors operators prefix-operator postfix-operator
简单地探索Swift,在Observable模式的通过仪式的背景下.我想要探索的是为了语法糖而实现前缀运算符.
class Observable<T> {
var value:T
init(_ v:T) {
value = v
}
}
postfix operator &* { }
postfix func &* (arg:Observable<T>) -> T {
return arg.value
}
Run Code Online (Sandbox Code Playgroud)
但是,编译器告诉我的-> T是use of an undeclared type.通用版真的不能用作返回类型吗?这里还有别的东西吗?
prefix-operator ×10
operators ×4
c++ ×3
javascript ×2
c ×1
c# ×1
generics ×1
increment ×1
indexing ×1
java ×1
new-operator ×1
parsing ×1
perl6 ×1
swift ×1
syntax ×1