Herzlich Willkommen, lieber Gast!
  Sie befinden sich hier:

  Forum » C / C++ (ANSI-Standard) » Konstruktor ohneInitialisierungssyntax

Forum | Hilfe | Team | Links | Impressum | > Suche < | Mitglieder | Registrieren | Einloggen
  Quicklinks: MSDN-Online || STL || clib Reference Grundlagen || Literatur || E-Books || Zubehör || > F.A.Q. < || Downloads   

Autor Thread - Seiten: > 1 <
000
23.02.2019, 15:00 Uhr
bagration




C++:
#include <iostream>
using namespace std;

class X {
   static int no;
public:
   X(int i = 4) {
     cout << ++no;
   }

   ~X() {
     cout << --no;
   }
};

int X::no{5};

int main() {
   X x, y{x};
   for (int i {0}; i<2; ++i) {
     X x{9};
   }

   X a;
   return 0;
}

/* Verstehe nicht ganz was hier im Konstruktor passiert. Werden die Objekte x und y mit irgendwelchen konkreten Werten initialisiert, oder mit bielibigen Werten? es fehlt die mir bekannte Syntax zur Initialisierung.
Auf jeden Fall soll 676767654 rauskommen.
Mir ist klar wie man auf die ersten zwei Ziffern kommt, aber was geschieht danach? Wenn die for-Schleife einsetzt sollte nach meiner Logik die dritte Ausgabe 8 sein, da einfach das no ausgegeben wird , das zuvor inkrementiert wurde. Aber das ist falsch.

Danke im voraus fuer die Hilfe!
*/

Dieser Post wurde am 23.02.2019 um 15:28 Uhr von ao editiert.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
001
23.02.2019, 15:03 Uhr
bagration



PS
Komme bis jetzt leider nicht drauf, wie man den Text editiert.
 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
002
25.02.2019, 13:39 Uhr
ao

(Operator)


Wenn du nicht verstehst, an welcher Stelle welche Ausgabe erzeugt wird, dann gib mal Zusatzinfos mit aus, z.B. so, das kann aufschlussreich sein:

C++:
#include <iostream>
using namespace std;

class X {
   static int no;
public:
   X(int i = 4) {
     cout << ++no;
   }

   ~X() {
     cout << --no;
   }
};

int X::no{5};

int main() {
   X x;
   std::cout << "A" << std::endl;
   X y{x};
   std::cout << "B" << std::endl;
   for (int i {0}; i<2; ++i) {
        X x{9};
        std::cout << "L" << i << std::endl;
   }

   X a;
   std::cout << "E" << std::endl;
   return 0;
}

 
Profil || Private Message || Suche Download || Zitatantwort || Editieren || Löschen || IP
Seiten: > 1 <     [ C / C++ (ANSI-Standard) ]  


ThWBoard 2.73 FloSoft-Edition
© by Paul Baecher & Felix Gonschorek (www.thwboard.de)

Anpassungen des Forums
© by Flo-Soft (www.flo-soft.de)

Sie sind Besucher: