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]

相关文章推荐

Linux: Unary Operator Expected (Bash Script Error)

From: http://www.codeunit.co.za/2009/09/03/linux-unary-operator-expected-bash-script-error/   If...

Codeforces - 133B - Unary (水题)

Codeforces - 133B - Unary (水题)原题链接:http://codeforces.com/problemset/problem/133/B B. Unary time...

标准模板库介绍(一元及二元函数对象Unary and Binary Function Objects)

第24章 标准模板库介绍 Chapter 24 Introducing the Standard Template Library 这一章介绍标准模板库-许多人认为这是最近几年被添加到C++的最重...

[C/C++标准库]_[初级]_[unary_function 和 binary_function]

unary function 一元和二元函数.

cliamte data operators(cdo)指南

  • 2015年07月19日 21:23
  • 1.39MB
  • 下载

mysql官方文档阅读笔记(四)Chapter 13 Functions and Operators第13章-函数和运算符

函数和运算符这一章节我打算先看了。因为用的比较多,功能很强大。微笑先打概了解下有哪些内容吧!...

Divide a number by 3 without using *, /, +, -, % operators

原帖: http://stackoverflow.com/questions/11694546/divide-a-number-by-3-without-using-operators ======...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:17.9.1 Unary operators
举报原因:
原因补充:

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