<?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[Felder von Objekten]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich bekomme in diesem Beispiel:</p>
<pre><code>#include &lt;iostream&gt;
using namespace std;

class Punkt {
	int x;
	int y;
public:
	Punkt(int a = 0, int b = 0) {
		x = a;
		y = b;
	}
	void printXY() {
		cout &lt;&lt; &quot;x=&quot; &lt;&lt; x &lt;&lt; &quot; y=&quot; &lt;&lt; y &lt;&lt; endl;
	}
};

int main() {
	Punkt pFeld[3] = { (11, 1), (12, 2), (13, 3) };

	for (int i = 0; i &lt; 3; i++)
		pFeld[i].printXY();
}
</code></pre>
<p>Diese Ausgabe:</p>
<pre><code>x=1 y=0
x=2 y=0
x=3 y=0
</code></pre>
<p>Ich verstehe nicht, wie es zu dieser Ausgabe kommt. Und muss für das Feld mit new (free) kein Speicher freigegeben werden?</p>
<p>Danke schon mal.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/323301/felder-von-objekten</link><generator>RSS for Node</generator><lastBuildDate>Thu, 16 Jul 2026 06:51:49 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/323301.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 25 Jan 2014 07:02:44 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Felder von Objekten on Sat, 25 Jan 2014 07:02:44 GMT]]></title><description><![CDATA[<p>Hallo,</p>
<p>ich bekomme in diesem Beispiel:</p>
<pre><code>#include &lt;iostream&gt;
using namespace std;

class Punkt {
	int x;
	int y;
public:
	Punkt(int a = 0, int b = 0) {
		x = a;
		y = b;
	}
	void printXY() {
		cout &lt;&lt; &quot;x=&quot; &lt;&lt; x &lt;&lt; &quot; y=&quot; &lt;&lt; y &lt;&lt; endl;
	}
};

int main() {
	Punkt pFeld[3] = { (11, 1), (12, 2), (13, 3) };

	for (int i = 0; i &lt; 3; i++)
		pFeld[i].printXY();
}
</code></pre>
<p>Diese Ausgabe:</p>
<pre><code>x=1 y=0
x=2 y=0
x=3 y=0
</code></pre>
<p>Ich verstehe nicht, wie es zu dieser Ausgabe kommt. Und muss für das Feld mit new (free) kein Speicher freigegeben werden?</p>
<p>Danke schon mal.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2379438</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2379438</guid><dc:creator><![CDATA[markus80]]></dc:creator><pubDate>Sat, 25 Jan 2014 07:02:44 GMT</pubDate></item><item><title><![CDATA[Reply to Felder von Objekten on Sat, 25 Jan 2014 07:19:36 GMT]]></title><description><![CDATA[<p>(11, 1) ist ein Ausdruck vom Typ int und hat den Wert 1. Somit steht da</p>
<pre><code>Punkt pFeld[3] = { 1,  2, 3 };
</code></pre>
<p>Ich hoffe jetzt ist die Ausgabe auch klar. Probier mal</p>
<pre><code>Punkt pFeld[3] = { {11, 1}, {12, 2}, {13, 3} };
</code></pre>
<p>(Kompilieren mit -std=c++11 -Wall -pedantic falls du gcc nutzt.) Nein, Speicher freigeben musst du hier keinen. Zudem nutzt man in C++ normalerweise Initialisierungslisten, das sieht dann so aus:</p>
<pre><code>class Punkt {
    int x;
    int y;
public:
    Punkt(int a = 0, int b = 0)
        : x(a) // &lt;-
        , y(b)
    {}
    void printXY() {
        cout &lt;&lt; &quot;x=&quot; &lt;&lt; x &lt;&lt; &quot; y=&quot; &lt;&lt; y &lt;&lt; endl;
    }
};
</code></pre>
<p>Und generell würde ich den Konstruktor bei sowas einfach weg lassen und x und y public machen. In C++ können auch structs Methoden haben. (Der einzige Unterschied zwischen struct und class ist, dass Member bei struct default public sind.)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2379439</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2379439</guid><dc:creator><![CDATA[cooky451]]></dc:creator><pubDate>Sat, 25 Jan 2014 07:19:36 GMT</pubDate></item><item><title><![CDATA[Reply to Felder von Objekten on Sat, 25 Jan 2014 07:17:23 GMT]]></title><description><![CDATA[<p>Der Ausruck (11, 1) ist dasselbe wie 11, 1.<br />
Das Komma ist der sog. Kommaoperator. Zuerst wird das links, dann das rechts ausgewertet. Das Ergebnis ist das, was rechts steht, in deinem Fapl also 1.<br />
Du 1 führt dann zun Aufruf von Punkt(1, 0).<br />
Richtig wäre Punkt(11, 1) in der Initialisierungsliste.<br />
Und nein, den Speicher musst du nicht freigeben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2379440</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2379440</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Sat, 25 Jan 2014 07:17:23 GMT</pubDate></item><item><title><![CDATA[Reply to Felder von Objekten on Sat, 25 Jan 2014 07:27:21 GMT]]></title><description><![CDATA[<p>Danke euch beiden!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2379441</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2379441</guid><dc:creator><![CDATA[markus80]]></dc:creator><pubDate>Sat, 25 Jan 2014 07:27:21 GMT</pubDate></item></channel></rss>