operators
De 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. |
Contenido |
[editar] Sobrecarga de operadores
[editar] Sintaxis
type operator op ( params ) ;
|
|||||||||
[editar] Explicación
- <tipo> es / son el tipo (s) de las variables .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> es el operador en particular (por ejemplo,
+
,+=
,<<
,>>
,&&
,||
,%
, etc) .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> es / son el nombre (s) de los parámetros requeridos (depende del operador) .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.
[editar] Restricciones
- Usted no puede crear nuevos operadores como
**
o&|
.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. - No todos los operadores pueden ser sobrecargadosOriginal: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. - Algunos operadores sólo se puede sobrecargar como miembros de la clase no estáticosOriginal: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. - Corto circuito de evaluación no funciona con operadores sobrecargadosOriginal: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.
[editar] Las llamadas del operador
Los operadores sobrecargados puede ser llamado usando la notación infija usual
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
o una función similar a la notación
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)
[editar] Ejemplo
#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
[editar] Vea también
Common operators | ||||||
---|---|---|---|---|---|---|
asignación | incrementNJdecrement | aritmética | lógico | comparación | memberNJaccess | otro |
a = b |
++a |
+a |
!a |
a == b |
a[b] |
a(...) |
Special operators | ||||||
static_cast convierte un tipo a otro tipo
compatible 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 convierte clase base virtual a class
derivada 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 convierte un tipo a otro compatible con diferentes 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 convierte el tipo de type
incompatible 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 asigna 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 desasigna 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 consulta el tamaño de un 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... consulta el tamaño de un parámetro de paquete (desde 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 consulta la información de una 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 comprueba si una expresión puede lanzar una excepción (desde 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 consultas requisitos de alineación de un (desde C++11) tipo
Original: alignof queries alignment requirements of a type (desde C++11) The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |