copy initialization
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. |
<metanoindex/>
Initialise un objet d'un autre objet
Original:
Initializes an object from another object
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.
Syntaxe
T object = other ;
|
(1) | ||||||||
f(other);
|
(2) | ||||||||
return other;
|
(3) | ||||||||
catch ( T other) ;
|
(4) | ||||||||
T array [ N ] = { other };
|
(5) | ||||||||
Explication
Initialisation de la copie est effectue dans les cas suivants:
Original:
Copy 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.
You can help to correct and verify the translation. Click here for instructions.
1)
quand une variable nomme (automatique, statique ou thread-local) est dclare avec l'initialiseur constitue d'un signe gal suivi d'une expression .
Original:
when a named variable (automatic, static, or thread-local) is declared with the initializer consisting of an equals sign followed by an expression.
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.
2)
en passant un argument une fonction par valeur
Original:
when passing an argument to a function by value
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.
3)
au retour d'une fonction qui retourne la valeur
Original:
when returning from a function that returns by value
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.
4)
lors de la capture d'une exception en valeur
Original:
when catching an exception by value
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.
5)
dans le cadre de initialisation d'agrgats, pour initialiser chaque lment pour lequel il est prvu une initialisation
Original:
as part of initialisation d'agrgats, to initialize each element for which an initializer is provided
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.
Les effets de l'initialisation de copie sont:
Original:
The effects of copy initialization are:
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.
- Si
Test un type de classe et le type de other est cv-inconditionnel de la versionTou une classe drive deT, les constructeurs deTsont examines et la meilleure correspondance est choisi par la rsolution de surcharge. Le constructeur est alors appele pour initialiser l'objet .Original:IfTis a class type and the type of other is cv-unqualified version ofTor a class derived fromT, the constructors ofTare 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.
- Si
Test un type de classe et le type de other est diffrent, ou siTn'est pas de type classe, mais le type de other est un type de classe, des squences dfinies par l'utilisateur de conversion qui peut convertir le type de otherTsont examins et le meilleur on est slectionn grce la rsolution de surcharge. Le rsultat de la conversion, qui est une prvalue temporaire du type de destination, est ensuite utilis pour diriger-initialiser l'objet. La dernire tape est gnralement limin et le rsultat de la fonction de conversion est construit directement dans la mmoire alloue pour l'objet cible, mais le constructeur de copie est ncessaire pour tre accessible mme si elle n'est pas utilise .Original:IfTis a class type, and the type of other is different, or ifTis non-class type, but the type of other is a class type, des squences dfinies par l'utilisateur de conversion that can convert from the type of other toTare examined and the best one is selected through overload resolution. The result of the conversion, which is a prvalue temporary of the destination type, is then used to diriger-initialiser the object. The last step is usually limin and the result of the conversion function is constructed directly in the memory allocated for the target object, but the copy constructor is required to be accessible even though it's not used.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
- Dans le cas contraire (si aucun
Tni le type de other sont les types de classes), conversions standard sont utiliss, si ncessaire, pour convertir la valeur de other la version cv-inconditionnel deT.Original:Otherwise (if neitherTnor the type of other are class types), conversions standard are used, if necessary, to convert the value of other to the cv-unqualified version ofT.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
Notes
Copier-initialisation est moins permissive que l'initialisation directe: la copie d'initialisation ne tient compte que non explicites constructeurs et les fonctions de conversion dfinis par l'utilisateur .
Original:
Copy-initialization is less permissive than direct-initialization: copy-initialization only considers non-explicit constructors and user-defined conversion functions.
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.
Si other est une expression rvalue, dplacer constructeur seront slectionns par la rsolution de surcharge et a appel au cours de la copie d'initialisation .
Original:
If other is an rvalue expression, dplacer constructeur will be selected by overload resolution and called during copy-initialization.
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.
Le signe gal,
=, dans la copie d'initialisation d'une variable nomme n'est pas li l'oprateur d'affectation. Les surcharges d'oprateur d'affectation n'ont aucun effet sur la copie d'initialisation .Original:
The equals sign,
=, in copy-initialization of a named variable is not related to the assignment operator. Assignment operator overloads have no effect on copy-initialization.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.
Exemple
#include <string>
#include <utility>
#include <memory>
int main()
{
std::string s = "test"; // OK: constructor is non-explicit
std::string s2 = std::move(s); // this copy-initialization performs a move
// std::unique_ptr<int> p = new int(1); // error: constructor is explicit
std::unique_ptr<int> p(new int(1)); // OK: direct-initialization
int n = 3.14; // floating-integral conversion
const int b = n; // const doesn't matter
int c = b; // ...either way
}