<?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[Destructor? kurze info bitte]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>Wie finde ich raus wie die der Destructor funktioniert. Hab hier nen code, aber kann nich genau verstehen wann der Destructor gerufen wird. Gibts da irgendwo vielleicht FAQ's zu? Ich hab nichts gefunden. Hier is der Code, und wie ihr ja sehen koennt gibt das Programm jedes mal wenn der destructor gerufen wird &quot;outta here&quot; aus.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;

using namespace std;

class Node {
	int data;
	Node *next;
public:
	Node() : data(0), next(0) { }
	Node(int e) : data(e), next(0) { }
	Node(int e, Node* n) : data(e), next(n) { }
	Node *getNext(void) const { return next; }
	int getData(void) const { return data; }
	void setData(int e) { data = e; }
	void setNext(Node* n) { next = n; }
};

class List {
	Node *start;
public:
	List() : start(0) { }
	void push_back(int e) {
		Node *newone = new Node(e);
		if (start == 0)
			start = newone;
		else {
			Node *temp = start;
			while (temp-&gt;getNext() != 0)
				temp = temp-&gt;getNext();
			temp-&gt;setNext(newone);
		}
		return;
	}
	friend ostream&amp; operator&lt;&lt;(ostream&amp;, const List&amp;);
};

ostream&amp; operator&lt;&lt;(ostream&amp; os, const List&amp; l) {
	os &lt;&lt; &quot;&lt; &quot;;
	Node *temp = l.start;
	while (temp) {
		os &lt;&lt; temp-&gt;getData() &lt;&lt; &quot; &quot;;
		temp = temp-&gt;getNext();
	}
	os &lt;&lt; &quot;&gt;&quot;;
	return os;
}

int main( ) {
	List a, b;
	cout &lt;&lt; &quot;Adding 10, 20, 30, 40, and 50 to a&quot; &lt;&lt; endl;
	a.push_back(10);
	a.push_back(20);
	a.push_back(30);
	a.push_back(40);
	a.push_back(50);
	cout &lt;&lt; &quot;List a is &quot; &lt;&lt; a &lt;&lt; endl;
	cout &lt;&lt; &quot;Assigning a to b&quot; &lt;&lt; endl;
	b = a;
	cout &lt;&lt; &quot;List b is &quot; &lt;&lt; b &lt;&lt; endl;
	cout &lt;&lt; &quot;Adding 60 and 70 to b&quot; &lt;&lt; endl;
	b.push_back(60);
	b.push_back(70);
	cout &lt;&lt; &quot;List b is now &quot; &lt;&lt; b &lt;&lt; endl;
	cout &lt;&lt; &quot;List a is now &quot; &lt;&lt; a &lt;&lt; endl;
	cout &lt;&lt; &quot;Declaring a new list c that is the same as a&quot; &lt;&lt; endl;
	List c(a);
	cout &lt;&lt; &quot;Adding 80 and 90 to c&quot; &lt;&lt; endl;
	c.push_back(80);
	c.push_back(90);
	cout &lt;&lt; &quot;List c is now&quot; &lt;&lt; c &lt;&lt; endl;
	cout &lt;&lt; &quot;List b is now&quot; &lt;&lt; b &lt;&lt; endl;
	cout &lt;&lt; &quot;List a is now&quot; &lt;&lt; a &lt;&lt; endl;

}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/140734/destructor-kurze-info-bitte</link><generator>RSS for Node</generator><lastBuildDate>Mon, 31 Aug 2026 08:55:43 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/140734.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 16 Mar 2006 21:40:29 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Destructor? kurze info bitte on Thu, 16 Mar 2006 21:40:29 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>Wie finde ich raus wie die der Destructor funktioniert. Hab hier nen code, aber kann nich genau verstehen wann der Destructor gerufen wird. Gibts da irgendwo vielleicht FAQ's zu? Ich hab nichts gefunden. Hier is der Code, und wie ihr ja sehen koennt gibt das Programm jedes mal wenn der destructor gerufen wird &quot;outta here&quot; aus.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;

using namespace std;

class Node {
	int data;
	Node *next;
public:
	Node() : data(0), next(0) { }
	Node(int e) : data(e), next(0) { }
	Node(int e, Node* n) : data(e), next(n) { }
	Node *getNext(void) const { return next; }
	int getData(void) const { return data; }
	void setData(int e) { data = e; }
	void setNext(Node* n) { next = n; }
};

class List {
	Node *start;
public:
	List() : start(0) { }
	void push_back(int e) {
		Node *newone = new Node(e);
		if (start == 0)
			start = newone;
		else {
			Node *temp = start;
			while (temp-&gt;getNext() != 0)
				temp = temp-&gt;getNext();
			temp-&gt;setNext(newone);
		}
		return;
	}
	friend ostream&amp; operator&lt;&lt;(ostream&amp;, const List&amp;);
};

ostream&amp; operator&lt;&lt;(ostream&amp; os, const List&amp; l) {
	os &lt;&lt; &quot;&lt; &quot;;
	Node *temp = l.start;
	while (temp) {
		os &lt;&lt; temp-&gt;getData() &lt;&lt; &quot; &quot;;
		temp = temp-&gt;getNext();
	}
	os &lt;&lt; &quot;&gt;&quot;;
	return os;
}

int main( ) {
	List a, b;
	cout &lt;&lt; &quot;Adding 10, 20, 30, 40, and 50 to a&quot; &lt;&lt; endl;
	a.push_back(10);
	a.push_back(20);
	a.push_back(30);
	a.push_back(40);
	a.push_back(50);
	cout &lt;&lt; &quot;List a is &quot; &lt;&lt; a &lt;&lt; endl;
	cout &lt;&lt; &quot;Assigning a to b&quot; &lt;&lt; endl;
	b = a;
	cout &lt;&lt; &quot;List b is &quot; &lt;&lt; b &lt;&lt; endl;
	cout &lt;&lt; &quot;Adding 60 and 70 to b&quot; &lt;&lt; endl;
	b.push_back(60);
	b.push_back(70);
	cout &lt;&lt; &quot;List b is now &quot; &lt;&lt; b &lt;&lt; endl;
	cout &lt;&lt; &quot;List a is now &quot; &lt;&lt; a &lt;&lt; endl;
	cout &lt;&lt; &quot;Declaring a new list c that is the same as a&quot; &lt;&lt; endl;
	List c(a);
	cout &lt;&lt; &quot;Adding 80 and 90 to c&quot; &lt;&lt; endl;
	c.push_back(80);
	c.push_back(90);
	cout &lt;&lt; &quot;List c is now&quot; &lt;&lt; c &lt;&lt; endl;
	cout &lt;&lt; &quot;List b is now&quot; &lt;&lt; b &lt;&lt; endl;
	cout &lt;&lt; &quot;List a is now&quot; &lt;&lt; a &lt;&lt; endl;

}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1017927</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1017927</guid><dc:creator><![CDATA[Endurance]]></dc:creator><pubDate>Thu, 16 Mar 2006 21:40:29 GMT</pubDate></item><item><title><![CDATA[Reply to Destructor? kurze info bitte on Thu, 16 Mar 2006 21:51:33 GMT]]></title><description><![CDATA[<p>Der Destruktor wird immer dann aufgerufen, wenn ein Objekt (das einen Destruktor hat ...) zerstört wird. Das passiert also:</p>
<ol>
<li>bei automatischen Objekten (auf dem Stack), wenn ihr Gültigkeitsbereich verlassen wird</li>
<li>bei dynamischen Objekten (auf dem Heap, mit new erzeugt), wenn sie 'delete't werden</li>
<li>bei statischen Objekten, wenn das Programm zuende ist</li>
<li>(wenn der Destruktor explizit aufgerufen wird, nur der Vollständigkeit halber, das geht, aber ist nichts für dich im Moment <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /> )</li>
</ol>
<p>Aber ich denke, du solltest das zum einem an einem geeigneteren Anschauungsobjekt mal nachvollziehen und zum anderen dir auch angucken, wann Objekte erzeugt werden, d.h. mal alles durchhecheln was ich oben so genannt habe (ausser 4.), dazu Call-by-Value, temporäre Objekte usw. usf.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1017934</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1017934</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Thu, 16 Mar 2006 21:51:33 GMT</pubDate></item><item><title><![CDATA[Reply to Destructor? kurze info bitte on Thu, 16 Mar 2006 21:53:22 GMT]]></title><description><![CDATA[<p>seh da irgendwie keinen destructor...</p>
<p>das ding wird immer aufgerufen, wenn ein objekt zerstört wird.</p>
<p>objekte werden entweder zerstört, wenn ihr gültigkeitsbereich verlassen wird oder wenn der speicher per delete freigegeben wird.</p>
<pre><code class="language-cpp">struct A;

void speicherleck()
{
 A a;
 A *b = new A;
}

void dollesding()
{
 A c;
 A *d = new A;
 delete d;
}

int main()
{
 speicherleck();
 dollesding();
}
</code></pre>
<p>nachdem speicherleck() durchgelaufen ist endet der gültigkeitsbereich der variablen a und b. a wird gelöscht und der desktruktor aufgerufen. b wird nicht gelöscht, also auch kein destruktor. da allerdings der handle futsch ist, hat man hier nen schickes speicherleck gebaut.</p>
<p>nachdem dollesding() durchgelaufen ist, wird c wiedermal gelöscht. der speicher von d wird freigeben (delete) und somit auch der destruktor aufgerufen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1017936</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1017936</guid><dc:creator><![CDATA[thordk]]></dc:creator><pubDate>Thu, 16 Mar 2006 21:53:22 GMT</pubDate></item><item><title><![CDATA[Reply to Destructor? kurze info bitte on Thu, 16 Mar 2006 22:12:24 GMT]]></title><description><![CDATA[<p>Das heisst ich bekomme 11 &quot;outta here&quot; outputs weil ich 11 elemente in meinen 3 List's 'a' 'b' und 'z' hab? 3 Elemente in a, 4 in b und 4 in z? Oh jetzt seh ich das erst mal, ich hab ja mehr als 11 outta heres im output. 3 oben und 11 unten, macht 14. Aber die unteren 11 hab ich schon richtig erkannt ne?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1017949</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1017949</guid><dc:creator><![CDATA[Endurance]]></dc:creator><pubDate>Thu, 16 Mar 2006 22:12:24 GMT</pubDate></item><item><title><![CDATA[Reply to Destructor? kurze info bitte on Thu, 16 Mar 2006 22:18:12 GMT]]></title><description><![CDATA[<p>Jetzt versteh ich irgendwie nichts mehr.<br />
Hab die Zeile</p>
<pre><code class="language-cpp">void Fun(List){cout &lt;&lt; &quot;Fun&quot;&lt;&lt; endl;}
</code></pre>
<p>um geandert zu</p>
<pre><code class="language-cpp">void Fun(List&amp;){cout &lt;&lt; &quot;Fun&quot;&lt;&lt; endl;}
</code></pre>
<p>und die Funktion ruft den destructor nich mehr, das heisst ich bekomme keine drei outta here mehr wenn ich in der main</p>
<pre><code class="language-cpp">a.Func(b);
</code></pre>
<p>aufrufe.<br />
Wie is das zu erklaeren?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1017953</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1017953</guid><dc:creator><![CDATA[Endurance]]></dc:creator><pubDate>Thu, 16 Mar 2006 22:18:12 GMT</pubDate></item><item><title><![CDATA[Reply to Destructor? kurze info bitte on Thu, 16 Mar 2006 22:54:13 GMT]]></title><description><![CDATA[<p>Das hier:</p>
<pre><code class="language-cpp">void Fun(List){cout &lt;&lt; &quot;Fun&quot;&lt;&lt; endl;}
</code></pre>
<p>legt eine Kopie des übergebenen List-Objekts an. Und die muß schließlich auch wieder destruiert werden.</p>
<p>Das hier:</p>
<pre><code class="language-cpp">void Fun(List&amp;){cout &lt;&lt; &quot;Fun&quot;&lt;&lt; endl;}
</code></pre>
<p>verwendet lediglich eine Referenz auf das übergebene Objekt. Es wird keine Kopie angelegt, folglich ist auch kein Destruktor-Aufruf notwendig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1017958</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1017958</guid><dc:creator><![CDATA[Z2]]></dc:creator><pubDate>Thu, 16 Mar 2006 22:54:13 GMT</pubDate></item><item><title><![CDATA[Reply to Destructor? kurze info bitte on Thu, 16 Mar 2006 23:06:52 GMT]]></title><description><![CDATA[<p>Macht Sinn!</p>
<p>Danke</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1017961</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1017961</guid><dc:creator><![CDATA[Endurance]]></dc:creator><pubDate>Thu, 16 Mar 2006 23:06:52 GMT</pubDate></item></channel></rss>