Initialization list

Nasza ocena:

3
Wyświetleń: 644
Komentarze: 0
Notatek.pl

Pobierz ten dokument za darmo

Podgląd dokumentu
Initialization list - strona 1 Initialization list - strona 2 Initialization list - strona 3

Fragment notatki:

Initialization list  in a definition of a constructor (and only there) we may use an initialization list T() : member(initializer) [, member(initializer) …] { } class point { double x, y; public: // … point():x(0.0), y(0.0) {}; }; inline point::point(double d) :x(d), y(d) { // all done } point::point(double x, double y) :x(x), y(y) // it is unambiguous !!! { // all done }  The only way of initializing const and reference members  We may also define the way of constructing virtual and direct base classes  Position in the initialization list is irrelevant  Initialization is performed in following order: - base classes  virtual  direct base classes in order of declaration - member variables in order of declaration (!) - constructor's body point::point(int x, int y):x(x), y(y) {}; // OK inline point::point(int i) :x(i), y(x) {}; // OK inline point::point(int i) :y(x), x(i) {}; // OK // point::point(int i) :y(i), x(y) {} — mistake !!! // first, y will be assigned to x, then i to y ... zobacz całą notatkę



Komentarze użytkowników (0)

Zaloguj się, aby dodać komentarz