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 ) ;
|
|||||||||
[править] Объяснение
- <type> является / являются тип (ы) переменных.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> является / являются имя (имена) необходимых параметров (зависит от оператора).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; }
Вывод:
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 преобразует тип совместимого типа с различными cv qualifiers
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. | ||||||