17.9.1 Unary operators

原创 2006年05月21日 02:49:00
The following rules apply to unary operator declarations, where T denotes
the class or struct type that contains
the operator declaration:
? A unary +, -, !, or ~ operator must take a single parameter of type T and
can return any type.
? A unary ++ or -- operator must take a single parameter of type T and must
return type T.
? A unary true or false operator must take a single parameter of type T and
must return type bool.
The signature of a unary operator consists of the operator token (+, -, !,
~, ++, --, true, or false) and the type
of the single formal parameter. The return type is not part of a unary
operator?s signature, nor is the name of the
formal parameter.
C# LANGUAGE SPECIFICATION
256
The true and false unary operators require pair-wise declaration. A
compile-time error occurs if a class
declares one of these operators without also declaring the other. The true
and false operators are described
further in §14.16.
[Example: The following example shows an implementation and subsequent
usage of operator++ for an integer
vector class:
public class IntVector
{
public int Length { ? } // read-only property
public int this[int index] { ? } // read-write indexer
public IntVector(int vectorLength) { ? }
public static IntVector operator++(IntVector iv) {
IntVector temp = new IntVector(iv.Length);
for (int i = 0; i < iv.Length; ++i)
temp[i] = iv[i] + 1;
return temp;
}
}
class Test
{
static void Main() {
IntVector iv1 = new IntVector(4); // vector of 4x0
IntVector iv2;
iv2 = iv1++; // iv2 contains 4x0, iv1 contains 4x1
iv2 = ++iv1; // iv2 contains 4x2, iv1 contains 4x2
}
}
Note how the operator method returns the value produced by adding 1 to the
operand, just like the postfix
increment and decrement operators(§14.5.9), and the prefix increment and
decrement operators (§14.6.5). Unlike
in C++, this method need not, and, in fact, must not, modify the value of
its operand directly. end example]

Overloading unary operators (C++ only)

You overload a unary operator with either a nonstatic member functionthat has no parameters, or a no...
  • xcrcug
  • xcrcug
  • 2009年03月12日 01:28
  • 162

error: lvalue required as unary ‘&’ operand

源码: printf("Now time is:%s\n",asctime(gmtime(&time(NULL)))); 提示错误: error: lvalue required as unary ‘...
  • sea1105
  • sea1105
  • 2016年05月09日 17:16
  • 1506

错误:的'unary*'实参类型无效(有'诠释')

错误:的'unary*'实参类型无效(有'诠释') c pointers 我有一个C程序: #include int main(){ int b = 10; //assig...
  • wangyin159
  • wangyin159
  • 2015年08月03日 15:16
  • 3163

unary operator expected解决办法

脚本刚开始如下: #!/bin/sh aa=$(grep "aa" aa) if [ 0 -ne $aa ] then     echo "aaaaaaaaaaaaa" else  ...
  • jeff06143132
  • jeff06143132
  • 2013年10月29日 13:08
  • 3546

熵编码 之 1. Unary coding

这种编码的思想很简单,将正整数N定义为:N-1个1后面紧跟着1个0,或者N-1个0后面紧跟着1个1。 例如: N code 1 0 2 10 3 110 4 1110 111110111...
  • brann
  • brann
  • 2014年05月14日 20:20
  • 1018

oracle: unary operator expected

在centos系统从root用户切换到oracle用的时候,总是提示 : -bash- : :[: unary operator expected 能切换成功,就是总提示上面这个,后来找到原因,在...
  • huang_xw
  • huang_xw
  • 2013年07月09日 12:36
  • 5385

关于“line 15: [: -gt: unary operator expected”报错的解决

今天写了个脚本内容大概是这样的: maxnum=100 num=`netstat -anlpt |grep $IP |wc -l` if [ $num -gt $maxnum ] then ...
  • qq_30063291
  • qq_30063291
  • 2016年04月14日 16:40
  • 2190

Swift在for循环中报错'++' is not a binary operator

最近刚开始学swift,遇到了一个非常诡异的问题。是在写for循环的时候出现语法错误。代码如下:for var i = 0; i < 10; i++{ println("hello world"...
  • abc649395594
  • abc649395594
  • 2015年08月08日 23:27
  • 1778

python TypeError: bad operand type for unary -: 'unicode'

sorted 字符串排序引起的
  • xp5xp6
  • xp5xp6
  • 2016年12月12日 19:37
  • 1710

shell脚本报错"[: =: unary operator expected"

今天用while写了一个循环,结果执行后就报错 [: =: unary operator expected上网查了之后便更改了脚本,在while后面多加一层[] ,就可以正确执行while [[ $d...
  • qq_31382921
  • qq_31382921
  • 2017年03月22日 15:48
  • 1275
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:17.9.1 Unary operators
举报原因:
原因补充:

(最多只允许输入30个字)