<?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[Referenzen auf Objekte übergeben ... Anfänger Problem &#x2F; Hilfe]]></title><description><![CDATA[<p>Hi, ich bin relativ neu in c++ und konnte mich eine sehr lange Zeit nicht mehr mit c++ beschäftigen wegen schule und weiters <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<p>naja da ich jetzt endlich wieder Zeit hab, hab ich wieder angefangen mit c++ und hab ein ernstes Problem (jedenfalls für mich)</p>
<p>Den Code den ich poste ist aus einem Beispiel von meinem Buch, jedoch versteh ich nicht was der Autor sagen will -.-</p>
<p>(Ich weiß dass das nicht der sauberste Code ist da alles in der main.cpp steht aber wie gesagt das ist jetzt ein Beispiel aus meinem Buch)</p>
<pre><code class="language-cpp">// -------------------------------------------------------------
// INCLUDES
// -------------------------------------------------------------

#include &quot;stdafx.h&quot;
#include &lt;iostream&gt;

// -------------------------------------------------------------
// KLASSEN DEKLARATIONEN
// -------------------------------------------------------------

class CSimpleCat {
public:
	CSimpleCat();  // Konstruktor
	CSimpleCat(CSimpleCat &amp;);  // Kopierkonstruktor
	~CSimpleCat();  // Destruktor

	int GetAge() const { return itsAge; }
	void SetAge(int age) { itsAge = age; }

private:
	int itsAge;
};

// -------------------------------------------------------------
// FUNKTIONS-PROTOTYPEN
// -------------------------------------------------------------

const CSimpleCat &amp;
	functionTwo(const CSimpleCat &amp;);

// -------------------------------------------------------------

using namespace std;

int main() 
{
	cout &lt;&lt; &quot;Eine Katze erzeugen....&quot; &lt;&lt; endl;
	CSimpleCat frisky;

	cout &lt;&lt; &quot;Frisky ist &quot; &lt;&lt; frisky.GetAge() &lt;&lt; &quot; Jahre alt!&quot; &lt;&lt; endl;

	int age = 5;

	frisky.SetAge(age);

	cout &lt;&lt; &quot;Frisky ist &quot; &lt;&lt; frisky.GetAge() &lt;&lt; &quot; Jahre alt!&quot; &lt;&lt; endl;
	cout &lt;&lt; &quot;functionTwo aufrufen....&quot; &lt;&lt; endl;

	functionTwo(frisky);

	cout &lt;&lt; &quot;Frisky ist : &quot; &lt;&lt; frisky.GetAge() &lt;&lt; &quot; Jahre alt!&quot; &lt;&lt; endl;

	getchar();
	return 0;
}

// -------------------------------------------------------------

CSimpleCat::CSimpleCat() {
	cout &lt;&lt; &quot;CSimpleCat Konstruktor...&quot; &lt;&lt; endl;
	itsAge = 1;
}

CSimpleCat::CSimpleCat(CSimpleCat &amp;) {
	cout &lt;&lt; &quot;CSimpleCat Kopierkonstruktor...&quot; &lt;&lt; endl;
}

CSimpleCat::~CSimpleCat() {
	cout &lt;&lt; &quot;CSimpleCat Destruktor...&quot; &lt;&lt; endl;
}

// -------------------------------------------------------------

const CSimpleCat &amp;
	functionTwo(const CSimpleCat &amp;theCat) {
	cout &lt;&lt; &quot;functionTwo. Rueckkehr....&quot; &lt;&lt; endl;
	cout &lt;&lt; &quot;Frisky ist jetzt &quot; &lt;&lt; theCat.GetAge() &lt;&lt; &quot; Jahre alt!&quot; &lt;&lt; endl;
	// theCat-&gt;SetAge(8);  const !
	return theCat;
}
</code></pre>
<p>also meine genaues Problem ist dass ich folgendes nicht wirklich verstehe</p>
<pre><code class="language-cpp">const CSimpleCat &amp;
	functionTwo(const CSimpleCat &amp;);
</code></pre>
<p>Das Argument der Funktion ist ja eine Referenz eines CSimpleCat Objekts, aber was ich nicht versteh ist der <strong>&amp;</strong> - Operator vor dem Funktionsname und was der genau bewirkt</p>
<p>Ich wär euch sehr dankbar wenn ihr mir dass schnell erklären könntet wenn es euch nichts ausmacht</p>
<p>vielen dank<br />
lg c4f</p>
<p><strong>EDIT:</strong> Also ich weiß dass man dadurch viele Aufrufe des Kopierkonstruktors und Destruktors spart, viel Speicher und dass das Program schneller läuft... aber es reicht mir nicht aus zu wissen dass es so ist aber ich viel es auch verstehen.<br />
Ich verstehe nicht warum noch die Funktion als Referenz auf das Objekt übergeben wird und was da genau &quot;passiert&quot;. Mir ist nur aufgefallen dass der Destruktor dann 1x weniger aufgerufen wird aber warum?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/218395/referenzen-auf-objekte-übergeben-anfänger-problem-hilfe</link><generator>RSS for Node</generator><lastBuildDate>Fri, 09 Oct 2026 06:29:13 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/218395.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 20 Jul 2008 15:01:26 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Referenzen auf Objekte übergeben ... Anfänger Problem &#x2F; Hilfe on Sun, 20 Jul 2008 16:05:04 GMT]]></title><description><![CDATA[<p>Hi, ich bin relativ neu in c++ und konnte mich eine sehr lange Zeit nicht mehr mit c++ beschäftigen wegen schule und weiters <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<p>naja da ich jetzt endlich wieder Zeit hab, hab ich wieder angefangen mit c++ und hab ein ernstes Problem (jedenfalls für mich)</p>
<p>Den Code den ich poste ist aus einem Beispiel von meinem Buch, jedoch versteh ich nicht was der Autor sagen will -.-</p>
<p>(Ich weiß dass das nicht der sauberste Code ist da alles in der main.cpp steht aber wie gesagt das ist jetzt ein Beispiel aus meinem Buch)</p>
<pre><code class="language-cpp">// -------------------------------------------------------------
// INCLUDES
// -------------------------------------------------------------

#include &quot;stdafx.h&quot;
#include &lt;iostream&gt;

// -------------------------------------------------------------
// KLASSEN DEKLARATIONEN
// -------------------------------------------------------------

class CSimpleCat {
public:
	CSimpleCat();  // Konstruktor
	CSimpleCat(CSimpleCat &amp;);  // Kopierkonstruktor
	~CSimpleCat();  // Destruktor

	int GetAge() const { return itsAge; }
	void SetAge(int age) { itsAge = age; }

private:
	int itsAge;
};

// -------------------------------------------------------------
// FUNKTIONS-PROTOTYPEN
// -------------------------------------------------------------

const CSimpleCat &amp;
	functionTwo(const CSimpleCat &amp;);

// -------------------------------------------------------------

using namespace std;

int main() 
{
	cout &lt;&lt; &quot;Eine Katze erzeugen....&quot; &lt;&lt; endl;
	CSimpleCat frisky;

	cout &lt;&lt; &quot;Frisky ist &quot; &lt;&lt; frisky.GetAge() &lt;&lt; &quot; Jahre alt!&quot; &lt;&lt; endl;

	int age = 5;

	frisky.SetAge(age);

	cout &lt;&lt; &quot;Frisky ist &quot; &lt;&lt; frisky.GetAge() &lt;&lt; &quot; Jahre alt!&quot; &lt;&lt; endl;
	cout &lt;&lt; &quot;functionTwo aufrufen....&quot; &lt;&lt; endl;

	functionTwo(frisky);

	cout &lt;&lt; &quot;Frisky ist : &quot; &lt;&lt; frisky.GetAge() &lt;&lt; &quot; Jahre alt!&quot; &lt;&lt; endl;

	getchar();
	return 0;
}

// -------------------------------------------------------------

CSimpleCat::CSimpleCat() {
	cout &lt;&lt; &quot;CSimpleCat Konstruktor...&quot; &lt;&lt; endl;
	itsAge = 1;
}

CSimpleCat::CSimpleCat(CSimpleCat &amp;) {
	cout &lt;&lt; &quot;CSimpleCat Kopierkonstruktor...&quot; &lt;&lt; endl;
}

CSimpleCat::~CSimpleCat() {
	cout &lt;&lt; &quot;CSimpleCat Destruktor...&quot; &lt;&lt; endl;
}

// -------------------------------------------------------------

const CSimpleCat &amp;
	functionTwo(const CSimpleCat &amp;theCat) {
	cout &lt;&lt; &quot;functionTwo. Rueckkehr....&quot; &lt;&lt; endl;
	cout &lt;&lt; &quot;Frisky ist jetzt &quot; &lt;&lt; theCat.GetAge() &lt;&lt; &quot; Jahre alt!&quot; &lt;&lt; endl;
	// theCat-&gt;SetAge(8);  const !
	return theCat;
}
</code></pre>
<p>also meine genaues Problem ist dass ich folgendes nicht wirklich verstehe</p>
<pre><code class="language-cpp">const CSimpleCat &amp;
	functionTwo(const CSimpleCat &amp;);
</code></pre>
<p>Das Argument der Funktion ist ja eine Referenz eines CSimpleCat Objekts, aber was ich nicht versteh ist der <strong>&amp;</strong> - Operator vor dem Funktionsname und was der genau bewirkt</p>
<p>Ich wär euch sehr dankbar wenn ihr mir dass schnell erklären könntet wenn es euch nichts ausmacht</p>
<p>vielen dank<br />
lg c4f</p>
<p><strong>EDIT:</strong> Also ich weiß dass man dadurch viele Aufrufe des Kopierkonstruktors und Destruktors spart, viel Speicher und dass das Program schneller läuft... aber es reicht mir nicht aus zu wissen dass es so ist aber ich viel es auch verstehen.<br />
Ich verstehe nicht warum noch die Funktion als Referenz auf das Objekt übergeben wird und was da genau &quot;passiert&quot;. Mir ist nur aufgefallen dass der Destruktor dann 1x weniger aufgerufen wird aber warum?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1550265</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1550265</guid><dc:creator><![CDATA[c4f]]></dc:creator><pubDate>Sun, 20 Jul 2008 16:05:04 GMT</pubDate></item><item><title><![CDATA[Reply to Referenzen auf Objekte übergeben ... Anfänger Problem &#x2F; Hilfe on Sun, 20 Jul 2008 17:34:36 GMT]]></title><description><![CDATA[<p>Kann mir wirklich keiner helfen? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<p>lg c4f</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1550364</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1550364</guid><dc:creator><![CDATA[c4f]]></dc:creator><pubDate>Sun, 20 Jul 2008 17:34:36 GMT</pubDate></item><item><title><![CDATA[Reply to Referenzen auf Objekte übergeben ... Anfänger Problem &#x2F; Hilfe on Sun, 20 Jul 2008 17:40:39 GMT]]></title><description><![CDATA[<p>Wenn das Argument keine Refernz wäre könntest du das alter zwar auf 8 Jahre ändern (Hast du auskommentiert), jedoch würde sich das nicht auf frisky auswirken, da das Argument kopiert werden würde (nur für diese Funktion):</p>
<pre><code class="language-cpp">const CSimpleCat &amp;
    functionTwo(const CSimpleCat theCat) { //Hier wird eine Kopie von frisky erstellt, die Referenz verhindert dies.
    cout &lt;&lt; &quot;functionTwo. Rueckkehr....&quot; &lt;&lt; endl;
    cout &lt;&lt; &quot;Frisky ist jetzt &quot; &lt;&lt; theCat.GetAge() &lt;&lt; &quot; Jahre alt!&quot; &lt;&lt; endl;
    theCat-&gt;SetAge(8);  const !
    return theCat;
</code></pre>
<p>Der Rückgabewert könnte hier void sein da du ihn nicht benutzt, jedoch ist er im Besipiel wieder eine Refernz, was bedeutet das du sowas machen könntest:</p>
<pre><code class="language-cpp">functionTwo(frisky)-&gt;setAge(101);
</code></pre>
<p>Auf diese Weise wird das Alter von frisky verändert. Wäre der Rückabetyp keine Refernz würdest du das alter einer temporären Kopie von frisky verändern, was dann keinen Einfluss auf frisky hätte.</p>
<p>Ich hoff das kann man verstehen ^^</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1550370</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1550370</guid><dc:creator><![CDATA[Pigeon]]></dc:creator><pubDate>Sun, 20 Jul 2008 17:40:39 GMT</pubDate></item><item><title><![CDATA[Reply to Referenzen auf Objekte übergeben ... Anfänger Problem &#x2F; Hilfe on Sun, 20 Jul 2008 18:00:34 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/8972">@Pigeon</a> : Danke das hat mir sehr geholfen und hat Licht ins Dunkle gebracht <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=";D"
      alt="😉"
    /><br />
Der Autor hatte es leider nicht genau erklärt deswegen hatte ich es nicht ganz kapiert, aber danke das hat mir jetzt viel geholfen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title="=)"
      alt="🙂"
    /></p>
<p>lg c4f</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1550393</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1550393</guid><dc:creator><![CDATA[c4f]]></dc:creator><pubDate>Sun, 20 Jul 2008 18:00:34 GMT</pubDate></item><item><title><![CDATA[Reply to Referenzen auf Objekte übergeben ... Anfänger Problem &#x2F; Hilfe on Sun, 20 Jul 2008 18:01:37 GMT]]></title><description><![CDATA[<p>Pigeon schrieb:</p>
<blockquote>
<p>Auf diese Weise wird das Alter von frisky verändert.</p>
</blockquote>
<pre><code>[b]const[/b] CSimpleCat &amp; functionTwo(const CSimpleCat &amp;);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1550394</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1550394</guid><dc:creator><![CDATA[Sicher?]]></dc:creator><pubDate>Sun, 20 Jul 2008 18:01:37 GMT</pubDate></item><item><title><![CDATA[Reply to Referenzen auf Objekte übergeben ... Anfänger Problem &#x2F; Hilfe on Sun, 20 Jul 2008 18:10:24 GMT]]></title><description><![CDATA[<p>Das const hab ich übersehn <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1550403</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1550403</guid><dc:creator><![CDATA[Pigeon]]></dc:creator><pubDate>Sun, 20 Jul 2008 18:10:24 GMT</pubDate></item><item><title><![CDATA[Reply to Referenzen auf Objekte übergeben ... Anfänger Problem &#x2F; Hilfe on Sun, 20 Jul 2008 18:20:55 GMT]]></title><description><![CDATA[<p>Pigeon schrieb:</p>
<blockquote>
<p>Das const hab ich übersehn <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
</blockquote>
<p>Ist nicht weiter tragisch <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="😉"
    /> mir war's auch aufgefalen aber hatte schon verstanden was du mir sagen wolltest <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
<p>Wie gesagt du hast mir sehr geholfen <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="😉"
    /></p>
<p>lg c4f</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1550412</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1550412</guid><dc:creator><![CDATA[c4f]]></dc:creator><pubDate>Sun, 20 Jul 2008 18:20:55 GMT</pubDate></item></channel></rss>