Espacios de nombres
Variantes
Acciones

direct initialization

De cppreference.com
< cpp‎ | language

 
 
Lenguaje C++
Los temas generales
Original:
General topics
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Control de flujo
Original:
Flow control
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Sentencias condicionales ejecución
Original:
Conditional execution statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Instrucciones de iteración
Original:
Iteration statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Ir declaraciones
Original:
Jump statements
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Funciones
Original:
Functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
declaración de la función
lambda declaración de la función
función de plantilla
especificador inline
especificaciones de excepción (obsoleto)
noexcept especificador (C++11)
Excepciones
Original:
Exceptions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Los espacios de nombres
Original:
Namespaces
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipos
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
decltype specifier (C++11)
Los especificadores
Original:
Specifiers
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
cv especificadores
almacenamiento duración especificadores
constexpr especificador (C++11)
auto especificador (C++11)
alignas especificador (C++11)
Inicialización
Original:
Initialization
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Los literales
Original:
Literals
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Expresiones
Original:
Expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
representaciones alternativas
Utilidades
Original:
Utilities
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Tipos
Original:
Types
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
typedef declaration
alias de tipo de declaración (C++11)
atributos (C++11)
Lanza
Original:
Casts
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
conversiones implícitas
const_cast conversion
static_cast conversion
dynamic_cast conversion
reinterpret_cast conversion
C-cast estilo y funcional
De asignación de memoria
Original:
Memory allocation
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Clases
Original:
Classes
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Clase propiedades específicas de la función
Original:
Class-specific function properties
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Funciones especiales miembro
Original:
Special member functions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Plantillas
Original:
Templates
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
clase de plantilla
función de plantilla
plantilla de especialización
paquetes de parámetros (C++11)
Varios
Original:
Miscellaneous
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Montaje en línea
 
Inicializa un objeto de conjunto explícito de argumentos del constructor .
Original:
Initializes an object from explicit set of constructor arguments.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

Contenido

[editar] Sintaxis

T object ( arg );

T object ( arg1, arg2, ... );

(1)
T object { arg };

T object { arg1, arg2, ... };

(2) (desde C++11)
T ( other )

T ( arg1, arg2, ... );

(3)
static_cast< T >( other ) (4)
new T(args, ...) (5)
Class::Class() : member(args, ...) {... (6)
[arg](){... (7) (desde C++11)

[editar] Explicación

Inicialización directa se realiza en las siguientes situaciones:
Original:
Direct initialization is performed in the following situations:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
1)
inicialización con una lista no vacía entre paréntesis de expresiones
Original:
initialization with a nonempty parenthesized list of expressions
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
2)
durante la secuencia de lista de inicialización, si no constuctors lista de inicialización que se proporcionan y un constructor de coincidencias es accesible, y todas las conversiones implícitas necesarias no son estrechamiento .
Original:
during lista de inicialización sequence, if no initializer-list constuctors are provided and a matching constructor is accessible, and all necessary implicit conversions are non-narrowing.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
3)
inicialización de un prvalue temporal por reparto funcional o con una lista de expresiones entre paréntesis
Original:
initialization of a prvalue temporary by reparto funcional or with a parenthesized expression list
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
4)
inicialización de un prvalue temporal por un expession static_cast
Original:
initialization of a prvalue temporary by a static_cast expession
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
5)
inicialización de un objeto con el tiempo de almacenamiento dinámico por una nueva expresión con un inicializador no-vacío
Original:
initialization of an object with dynamic storage duration by a new-expression with a non-empty initializer
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
6)
inicialización de una base o un miembro no estático por inicializador lista constructor
Original:
initialization of a base or a non-static member by constructor inicializador lista
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
7)
inicialización de miembros de objetos de cierre de las variables capturadas por copia en un lambda-expresión
Original:
initialization of closure object members from the variables caught by copy in a lambda-expression
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Los efectos de la inicialización directa son:
Original:
The effects of direct initialization are:
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
  • Si T es un tipo de clase, los constructores de T se examinan y el mejor partido es seleccionado por resolución de sobrecarga. El constructor se llama entonces para inicializar el objeto .
    Original:
    If T is a class type, the constructors of T are examined and the best match is selected by overload resolution. The constructor is then called to initialize the object.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.
  • De lo contrario, si T es un tipo no-clase, conversiones estándar se utilizan, si es necesario, para convertir el valor de other a la versión cv-incondicional de T .
    Original:
    Otherwise, if T is a non-class type, conversiones estándar are used, if necessary, to convert the value of other to the cv-unqualified version of T.
    The text has been machine-translated via Google Translate.
    You can help to correct and verify the translation. Click here for instructions.

[editar] Notas

Direct-inicialización es más permisiva que la copia de inicialización: copy-inicialización sólo tiene en cuenta no explícitas constructores y funciones definidas por el usuario de conversión, mientras que directa-inicialización considera todos los constructores y las secuencias de conversión implícitas .
Original:
Direct-initialization is more permissive than copy-initialization: copy-initialization only considers non-explicit constructors and user-defined conversion functions, while direct-initialization considers all constructors and implicit conversion sequences.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.

[editar] Ejemplo

#include <string>
#include <iostream>
#include <memory>
 
struct Foo {
    int mem;
    explicit Foo(int n) : mem(n) {}
};
 
int main()
{
    std::string s1("test"); // constructor from const char*
    std::string s2(10, 'a');
 
    std::unique_ptr<int> p(new int(1)); // OK: explicit constructors allowed
//  std::unique_ptr<int> p = new int(1); // error: constructor is explicit
 
    Foo f(2); // f is direct-initialized:
              // constructor parameter n is copy-initialized from the rvalue 2
              // f.mem is direct-initialized from the parameter n
//  Foo f2 = 2; // error: constructor is explicit
 
    std::cout << s1 << ' ' << s2 << ' ' << *p << ' ' << f.mem  << '\n';
}

Output:

test aaaaaaaaaa 1 2

[editar] Ver también