operators
来自cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
目录 |
[编辑] 运算符重载
[编辑] 语法
type operator op ( params ) ;
|
|||||||||
[编辑] 解释
- <类型> /(s)的变量的类型.Original:<type> is/are the type(s) of the variables.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - <op>是在特定的操作(例如
+,+=,<<,>>,&&,||,%等).Original:<op> is the particular operator (e.g.+,+=,<<,>>,&&,||,%, etc.).The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 与<params> /(s)的所需的参数的名称(取决于操作员).Original:<params> is/are the name(s) of the required parameters (depends on the operator).The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 限制
- 您不能创建新的运营商,如
**或&|.Original:You cannot create new operators such as**or&|.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 并非所有的运营商都可以被重载Original:Not all operators can be overloadedThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 一些运算符只能重载为类非静态成员Original:Some operators can only be overloaded as non-static class membersThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions. - 不使用重载运算符的短路计算Original:Short-circuit evaluation doesn't work with overloaded operatorsThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
[编辑] 操作者呼叫
使用通常的中缀符号可以被称为重载操作符
Original:
Overloaded operators can be called using the usual infix notation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
a+b或函数符号
Original:
or a function-like notation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
operator+(a,b)
[编辑] 为例
#include <iostream> using namespace std; class Fraction{ private: int numerator, denominator; public: Fraction(int n, int d): numerator(n), denominator(d) {} // Note that the keyword operator combined with an actual // operator is used as the function name friend ostream& operator<<(ostream&, Fraction&); }; ostream& operator<<(ostream& out, Fraction& f){ out << f.numerator << '/' << f.denominator; return out; } int main(){ Fraction f1(3, 8); Fraction f2(1, 2); cout << f1 << endl; cout << 3 << ' ' << f2 << endl; return 0; }
Output:
3/8 3 1/2
[编辑] 见也
| Common operators | ||||||
|---|---|---|---|---|---|---|
| 分配 | incrementNJdecrement | 算术 | 合乎逻辑的 | 比较 | memberNJaccess | 其他 |
|
a = b |
++a |
+a |
!a |
a == b |
a[b] |
a(...) |
| Special operators | ||||||
|
static_cast将一种类型转换到另一个兼容的类型
Original: static_cast converts one type to another compatible type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. dynamic_cast将虚基类到派生class
Original: dynamic_cast converts virtual base class to derived class The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. const_cast转换类型兼容型,与不同的cvqualifiers
Original: The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. reinterpret_cast转换类型不兼容的type
Original: reinterpret_cast converts type to incompatible type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. new个分配memory
Original: new allocates memory The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. delete会释放memory
Original: delete deallocates memory The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. sizeof查询的大小的type
Original: sizeof queries the size of a type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. sizeof...查询的大小的参数组(C++11 起)
Original: The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. typeid查询的类型一个type
信息 Original: typeid queries the type information of a type The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. noexcept进行检查,,如果的表达可以抛出一个异常(C++11 起)
, Original: The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. alignof查询类型(C++11 起)对齐要求
Original: alignof queries alignment requirements of a type (C++11 起) The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | ||||||

