<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Konstruktor&#x2F;Destruktor - Problem]]></title><description><![CDATA[<p>Hey Leute<br />
Beschäftige mich zur Zeit ein bisschen mit C++-Programmierung und check die Abläufe mit den Konstruktorn noch nicht. Also hier mal ein Beispielprogramm:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
using namespace std;

class Test {
    private:
        int abc;
    public:
        Test(int i=9);
        ~Test();
        int ausgabe() { return abc; };
};

Test::Test(int i) {
    cout &lt;&lt; &quot;Konstruktor\n&quot;;
    if(i &gt; 12){
        abc=12;

    }
    else
        abc=i;
}

Test::~Test() {
    cout &lt;&lt; &quot;Destruktor\n&quot;;
}

main(){
    Test x(7);
    Test y;
    Test z(13);
    cout &lt;&lt; &quot; &quot; &lt;&lt; x.ausgabe() &lt;&lt; &quot;\n&quot;;
    cout &lt;&lt; &quot; &quot; &lt;&lt; y.ausgabe() &lt;&lt; &quot;\n&quot;;
    cout &lt;&lt; &quot; &quot; &lt;&lt; z.ausgabe() &lt;&lt; &quot;\n&quot;;
    return 0;
}
</code></pre>
<p>Beim Ausführn erscheint folgendes:</p>
<blockquote>
<p>Konstruktor<br />
Konstruktor<br />
Konstruktor<br />
7<br />
9<br />
12<br />
Destruktor<br />
Destruktor<br />
Destruktor</p>
</blockquote>
<p>Hier meine Fragen:<br />
Warum wird 3mal Konstruktor/Destruktor ausgegeben?(Warum nicht nur die Werte?)<br />
Warum gerade diese 3 Werte?</p>
<p>Freu mich schon auf eure Antworten und bedank mich schon mal für eure Hilfe.</p>
<p>greetz</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/257729/konstruktor-destruktor-problem</link><generator>RSS for Node</generator><lastBuildDate>Wed, 09 Sep 2026 23:17:29 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/257729.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 02 Jan 2010 15:31:07 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Konstruktor&#x2F;Destruktor - Problem on Sat, 02 Jan 2010 15:32:28 GMT]]></title><description><![CDATA[<p>Hey Leute<br />
Beschäftige mich zur Zeit ein bisschen mit C++-Programmierung und check die Abläufe mit den Konstruktorn noch nicht. Also hier mal ein Beispielprogramm:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
using namespace std;

class Test {
    private:
        int abc;
    public:
        Test(int i=9);
        ~Test();
        int ausgabe() { return abc; };
};

Test::Test(int i) {
    cout &lt;&lt; &quot;Konstruktor\n&quot;;
    if(i &gt; 12){
        abc=12;

    }
    else
        abc=i;
}

Test::~Test() {
    cout &lt;&lt; &quot;Destruktor\n&quot;;
}

main(){
    Test x(7);
    Test y;
    Test z(13);
    cout &lt;&lt; &quot; &quot; &lt;&lt; x.ausgabe() &lt;&lt; &quot;\n&quot;;
    cout &lt;&lt; &quot; &quot; &lt;&lt; y.ausgabe() &lt;&lt; &quot;\n&quot;;
    cout &lt;&lt; &quot; &quot; &lt;&lt; z.ausgabe() &lt;&lt; &quot;\n&quot;;
    return 0;
}
</code></pre>
<p>Beim Ausführn erscheint folgendes:</p>
<blockquote>
<p>Konstruktor<br />
Konstruktor<br />
Konstruktor<br />
7<br />
9<br />
12<br />
Destruktor<br />
Destruktor<br />
Destruktor</p>
</blockquote>
<p>Hier meine Fragen:<br />
Warum wird 3mal Konstruktor/Destruktor ausgegeben?(Warum nicht nur die Werte?)<br />
Warum gerade diese 3 Werte?</p>
<p>Freu mich schon auf eure Antworten und bedank mich schon mal für eure Hilfe.</p>
<p>greetz</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1830955</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1830955</guid><dc:creator><![CDATA[piscoster]]></dc:creator><pubDate>Sat, 02 Jan 2010 15:32:28 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor&#x2F;Destruktor - Problem on Sat, 02 Jan 2010 15:47:01 GMT]]></title><description><![CDATA[<p>Es wird für jedes Objekt, das du anlegst der Konstruktor aufgerufen. Da du 3 Objekte anlegst, sind es auch 3 Konstruktor -Aufrufe. Diese 3 Objekte weder auch wieder zerstört. Es wird also für jedes der 3 Objekte auch der Destruktor aufgerufen.</p>
<p>Die Variable abc wird im Konstruktor gesetzt. Ist der übergebene Parameter größer als 12, so wird abc auf 12 gesetzt. Ist er hingegen kleiner als 12, wird abc der übergebene Wert zugewiesen.</p>
<p>Im ersten Fall übergibst du 7. Also wird abc auf 7 gesetzt. Beim Aufruf der Funktion ausgabe erscheint somit 7.</p>
<p>Im zweiten Fall ist gar kein Wert übergeben. Jedoch hat der Konstruktor einen Standardwert 9. abc wird somit auf 9 gesetzt.</p>
<p>Im dritten Fall wird 13 übergeben, da dies größer als 12 ist, wird abc auf 12 gesetzt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1830970</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1830970</guid><dc:creator><![CDATA[asdasdasd]]></dc:creator><pubDate>Sat, 02 Jan 2010 15:47:01 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor&#x2F;Destruktor - Problem on Sat, 02 Jan 2010 15:48:01 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;iostream&gt;
using namespace std;

class Test {
    private:
        int abc;
    public:
        Test(int i=9);
        ~Test();
        int ausgabe() { return abc; };
};

Test::Test(int i) {
    cout &lt;&lt; &quot;Konstruktor\n&quot;;
    if(i &gt; 12){
        abc=12;

    }
    else
        abc=i;
}

Test::~Test() {
    cout &lt;&lt; &quot;Destruktor\n&quot;;
}

main(){ // besser: int main(), wegen Standardkonformität
    Test x(7); // Konstruktor für Objekt x wird aufgerufen
    Test y;    // Konstruktor für Objekt y wird aufgerufen
    Test z(13);// Konstruktor für Objekt z wird aufgerufen
    cout &lt;&lt; &quot; &quot; &lt;&lt; x.ausgabe() &lt;&lt; &quot;\n&quot;; //
    cout &lt;&lt; &quot; &quot; &lt;&lt; y.ausgabe() &lt;&lt; &quot;\n&quot;; // Ausgabe der Werte
    cout &lt;&lt; &quot; &quot; &lt;&lt; z.ausgabe() &lt;&lt; &quot;\n&quot;; //
    return 0; // Gültigkeitsbereich von x, y und z endet -&gt; Automatischer Destruktoraufruf aller Objekte in umgekehrter Reihenfolge
}
</code></pre>
<p>piscoster schrieb:</p>
<blockquote>
<p>Beim Ausführn erscheint folgendes:</p>
<blockquote>
<p>Konstruktor<br />
Konstruktor<br />
Konstruktor<br />
7<br />
9<br />
12<br />
Destruktor<br />
Destruktor<br />
Destruktor</p>
</blockquote>
<p>Hier meine Fragen:<br />
Warum wird 3mal Konstruktor/Destruktor ausgegeben?(Warum nicht nur die Werte?)</p>
</blockquote>
<p>- Weil der Konstruktor automatisch aufgerufen wird, wenn ein neues Objekt erstellt wird (hier drei Objekte, daher 3x Konstruktor); genauso verfährt es mit dem Desktruktor, sobald das Objekt seinen Gültigkeitsbereich verlässt (Ende von main())</p>
<p>piscoster schrieb:</p>
<blockquote>
<p>Warum gerade diese 3 Werte?</p>
</blockquote>
<p>Weil du den Objekten diese Werte übergibst, bzw. weil dem Member abc im Konstruktor der jeweilige Wert zugewiesen wird.<br />
(Standardkonstruktor: 9, ansonsten übergebener Wert, falls kleiner oder gleich 12 (so hast du es innerhalb der Klasse bestimmt. ;))</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1830972</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1830972</guid><dc:creator><![CDATA[helfer]]></dc:creator><pubDate>Sat, 02 Jan 2010 15:48:01 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor&#x2F;Destruktor - Problem on Sat, 02 Jan 2010 15:50:53 GMT]]></title><description><![CDATA[<p>Falls du das noch immer nicht checkst. Bau dir Ausgaben ein:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
using namespace std;

class Test {
    private:
        int abc;
    public:
        Test(int i=9);
        ~Test();
        int ausgabe() { return abc; };
};

Test::Test(int i) {
	cout &lt;&lt; &quot;Konstruktor: Es wurde &quot; &lt;&lt; i &lt;&lt; &quot; uebergeben.\n&quot;;

    if(i &gt; 12)
	{
		cout &lt;&lt; &quot;i ist &quot; &lt;&lt; i &lt;&lt; &quot; und damit groesser als 12, also wird abc auf 12 gesetzt.\n&quot;;
        abc=12;
    }
    else
    {
        cout &lt;&lt; &quot;i ist kleiner als 12, also wird abc auf &quot; &lt;&lt; i &lt;&lt; &quot; gesetzt.\n&quot;;
        abc=i;
    }

	cout &lt;&lt; &quot;\n&quot;;
}

Test::~Test() {
    cout &lt;&lt; &quot;\nDestruktor von Objekt mit abc = &quot; &lt;&lt; abc &lt;&lt; &quot;\n&quot;;
}

int main()
{
    Test x(7);
    Test y;
    Test z(13);

    cout &lt;&lt; &quot; &quot; &lt;&lt; x.ausgabe() &lt;&lt; &quot;\n&quot;;
    cout &lt;&lt; &quot; &quot; &lt;&lt; y.ausgabe() &lt;&lt; &quot;\n&quot;;
    cout &lt;&lt; &quot; &quot; &lt;&lt; z.ausgabe() &lt;&lt; &quot;\n&quot;;

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1830975</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1830975</guid><dc:creator><![CDATA[asdasdasd]]></dc:creator><pubDate>Sat, 02 Jan 2010 15:50:53 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor&#x2F;Destruktor - Problem on Sat, 02 Jan 2010 15:59:42 GMT]]></title><description><![CDATA[<p>also erstmal thx euch allen für eure schnelle Hilfe!!!!</p>
<p>So wie ich das sehe muss der Konstruktor erst vollständig 3 Mal abgearbeitet werden um die 3 Werte(x,y,z) auszugeben... und der Destruktor schließ diesen Prozess dann. Somit ist der Konstruktor so eine Art von Funktion...</p>
<p>greetz<br />
pisco</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1830981</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1830981</guid><dc:creator><![CDATA[piscoster]]></dc:creator><pubDate>Sat, 02 Jan 2010 15:59:42 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor&#x2F;Destruktor - Problem on Sat, 02 Jan 2010 16:02:21 GMT]]></title><description><![CDATA[<p>piscoster schrieb:</p>
<blockquote>
<p>also erstmal thx euch allen für eure schnelle Hilfe!!!!</p>
<p>So wie ich das sehe muss der Konstruktor erst vollständig 3 Mal abgearbeitet werden um die 3 Werte(x,y,z) auszugeben... und der Destruktor schließ diesen Prozess dann. Somit ist der Konstruktor so eine Art von Funktion...</p>
<p>greetz<br />
pisco</p>
</blockquote>
<p>Im Gegensatz zu einer Funktion kann ein Konstruktor/Destruktor aber keine Werte zurückgeben. Aber ansonsten verhält er sich ähnlich. Und der Konstruktor dient zur Erzeugung (Konstruktion) des Objekts. Erst wenn er fertig ist, kann das Objekt benutzt werden.</p>
<p>Der Sinn von Konstruktor und Destruktor ist relativ einfach. Es gibt Objekte, die Variablen initialisieren müssen bevor sie korrekt arbeiten. Außerdem gibt es Objekte, die Speicher belegen, der am Ende der &quot;Lebensdauer&quot; wieder freigegeben werden muss.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1830985</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1830985</guid><dc:creator><![CDATA[asdasdasd]]></dc:creator><pubDate>Sat, 02 Jan 2010 16:02:21 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor&#x2F;Destruktor - Problem on Sat, 02 Jan 2010 16:04:24 GMT]]></title><description><![CDATA[<p>asdasdasd schrieb:</p>
<blockquote>
<p>piscoster schrieb:</p>
<blockquote>
<p>also erstmal thx euch allen für eure schnelle Hilfe!!!!</p>
<p>So wie ich das sehe muss der Konstruktor erst vollständig 3 Mal abgearbeitet werden um die 3 Werte(x,y,z) auszugeben... und der Destruktor schließ diesen Prozess dann. Somit ist der Konstruktor so eine Art von Funktion...</p>
<p>greetz<br />
pisco</p>
</blockquote>
<p>Im Gegensatz zu einer Funktion kann ein Konstruktor/Destruktor aber keine Werte zurückgeben. Aber ansonsten verhält er sich ähnlich. Und der Konstruktor dient zur Erzeugung (Konstruktion) des Objekts. Erst wenn er fertig ist, kann das Objekt benutzt werden.</p>
<p>Der Sinn von Konstruktor und Destruktor ist relativ einfach. Es gibt Objekte, die Variablen initialisieren müssen bevor sie korrekt arbeiten. Außerdem gibt es Objekte, die Speicher belegen, der am Ende der &quot;Lebensdauer&quot; wieder freigegeben werden muss.</p>
</blockquote>
<p>Du kannst die Lebensdauer übrigens &quot;künstlich&quot; verkürzen.</p>
<pre><code class="language-cpp">//...

int main()
{
 test a; // Lebt bis zum Ende von main

 { // Block Anfang -&gt;

  Test b; // Lebt bis zum Ende diess Blocks

 } // &lt;- Block Ende

 Test c;  // Lebt bis zum Ende von main
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1830987</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1830987</guid><dc:creator><![CDATA[asdasdasd]]></dc:creator><pubDate>Sat, 02 Jan 2010 16:04:24 GMT</pubDate></item><item><title><![CDATA[Reply to Konstruktor&#x2F;Destruktor - Problem on Sat, 02 Jan 2010 16:04:25 GMT]]></title><description><![CDATA[<p>Nur damit keine Missverständnisse entstehen: Konstruktoren und Destruktoren <strong>sind</strong> Funktionen, und zwar Memberfunktionen der Klasse.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1830988</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1830988</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Sat, 02 Jan 2010 16:04:25 GMT</pubDate></item></channel></rss>