Typedef
typedef es una palabra reservada en el lenguaje de programación C y C++. Su función es asignar un nombre alternativo a tipos existentes, a menudo cuando su declaración normal es aparatosa, potencialmente confusa o probablemente variable de una implementación a otra.
Ejemplos de uso
Ejemplo básico de typedef
Considere este código:
#include <stdio.h>
int main(void)
{
int notas;
notas=100;
return 0;
}
Ahora considere esto:
#include <stdio.h>
int main()
{
typedef int nota_alumno_t;
nota_alumno_t notas;
notas=100;
return -1;
}
Ambas secciones de código hacen lo mismo: crean un tipo int (notas) y le dan un valor de 100. El método para hacer esto en la segunda sección hace que sea más legible porque la declaración typedef hace que nota_alumno_t signifique lo mismo que int. En este ejemplo, la variable notas guarda las "notas" de un estudiante, así que definir notas como una variable de tipo nota_alumno_t le da al nombre de esa variable un contexto.
Ejemplo con struct
Uno de los usos principales del typedef es la creación de tipos struct.
Un ejemplo
struct persona
{
int edad;
char *name;
};
Para utilizar este struct, tenemos que añadir la palabra reservada struct antes de persona
struct persona alex;
Con typedef, podemos crear un nuevo tipo persona, para que el uso de los struct sea más ergonómico.
typedef struct persona persona_t;
Ahora, para crear una variable de tipo persona, podemos hacerlo así:
persona_t alex;
Esto es más legible porque no requiere la palabra reservada struct antes de cada variable de tipo persona.
Uso en C++
std::vector<std::pair<std::string, int> > valores;
for (std::vector<std::pair<std::string, int> >::const_iterator i = valores.begin(); i != valores.end(); ++i)
{
std::pair<std::string, int> const & t = *i;
// hacer alguna tarea
}
y
typedef std::pair<std::string, int> valor_t;
typedef std::vector<valor_t> valores_t;
valores_t valores;
for (valores_t::const_iterator i = valores.begin(); i != valores.end(); ++i)
{
valor_t const & t = *i;
// hacer alguna tarea
}
Enlaces externos
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.