2017-07-14 2 views
4

Was sind die Unterschiede zwischen dieser Art der Initialisierung in C++;Unterschiede zwischen der Initialisierung von Int in C++ mit Doppel

int a = 0; 
int a{}; 
int(); 

warum dieser Code int a{3.14} hol mir Fehler, aber dieses int a = 3.14 oder dieses int a(3.14) nicht

+3

nachschlagen nützlich sein [Umwandlungen Verengung] (http://en.cppreference.com/w/cpp/language/list_initialization#Narrowing_conversions) –

Antwort

2

int a = 0; und int a (0); keinen Unterschied im maschinengenerierten Code. Sie sind gleich.

Im Anschluss an die in Visual Studio generierte Assembly Code

int a = 10; // mov dword ptr [a],0Ah 
int b(10); // mov dword ptr [b],0Ah 

aber int a{} wegen Verengung Konvertierungen ein wenig anders ist, die einige Liste initialisieren

diese sind von c++ reference site verbieten:

Verkleinerte Umrechnungen

list-Initialisierung schränkt die zulässigen impliziten Konvertierungen von Verbot der folgenden:

conversion from a floating-point type to an integer type 

conversion from a long double to double or to float and conversion from double to float, except where the source is a constant expression 

und Überlauf nicht

conversion from an integer type to a floating-point type, except where the source is a constant expression whose value can be stored 

genau in dem Zieltyp

conversion from integer or unscoped enumeration type to integer type that cannot represent all values of the original, except where 

Quelle eine Konstante auftritt Ausdruck, dessen Wert genau in der Zielart

01 gespeichert werden kann

Ich wünschte, diese Antwort würde

1

a{3.14} wirft einen Fehler, weil Sie nicht die type angegeben haben,

int a(3.14) // initial value: 3.14, you could use {} also intead of() gewohnt, weil Sie sagten, es ist ganzzahlig ..

Ich werde Ihnen eine Erklärung geben, und ich hoffe, es wird Ihnen klarer sein:

// Bog-standard declaration. 


    int a; 


// WRONG - this declares a function. 

    int a(); 

// Bog-standard declaration, with constructor arguments. 
// (*) 

    int a(1, 2, 3... args); 

// Bog-standard declaration, with *one* constructor argument 
// (and only if there's a matching, _non-explicit_ constructor). 
// (**) 

    int a = 1; 

// Uses aggregate initialisation, inherited from C. 
// Not always possible; depends on layout of T. 

    int a = {1, 2, 3, 4, 5, 6}; 

// Invoking C++0x initializer-list constructor. 

    int a{1, 2, 3, 4, 5, 6}; 

// This is actually two things. 
// First you create a [nameless] rvalue with three 
// constructor arguments (*), then you copy-construct 
// a [named] T from it (**). 

    int a = T(1, 2, 3); 

// Heap allocation, the result of which gets stored 
// in a pointer. 

    int* a = new T(1, 2, 3); 

// Heap allocation without constructor arguments. 

    int* a = new T; 
+0

Wie funktioniert Liste Initialisierungsarbeit für eine Variable mit nur einem Wert? Zum Beispiel 'int a = {...}; int a {...}; ' –

4

Es Liste Initialisierung (C++ 11) genannt:

int foo = 0; // Initialize foo with 0 
int foo{}; // Initialize foo with foo's type (int) default value, which is 0 
int foo(); // Function declaration 

int bar = 5.f; // Initialize bar with 5 (narrowing conversion from floating point) 
int bar{5.f}; // Doesn't compile, because there is a loss of data when casting a float to an int 
int bar(5.f); // Initialize bar with 5 (narrowing conversion from floating point) 

jedoch:

float f{5}; // Okay, because there is no loss of data when casting an int to a float 
+0

Ist' int foo(); 'eine Variablenerstellung oder eine Funktionsdeklaration? Mein Verständnis ist, dass es eine Funktionsdeklaration sein würde. –

1

Diese beiden Linien sind äquivalent

int i = 42; 
int j(42); 

Wie für verspannt Initialisierung Es ist ein C++ - Feature, das in erschienen ist C++ 11 Standard. Aus diesem Grund muss es nicht mit C-Standard kompatibel sein, und es kommt daher mit strengeren Typsicherheitsgarantien. Es verbietet nämlich eine implizite verengende Konvertierung.

int i{ 42 }; 
int j{ 3.14 }; // fails to compile 
int k{ static_cast<int>(2.71) }; // fine 

Hoffe, dass hilft. Lassen Sie es mich wissen, wenn Sie mehr Informationen benötigen.

Verwandte Themen