<?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[Copy Constructor: Vererbung &amp;amp; dynamische Bindung]]></title><description><![CDATA[<p>Tut mir leid, dass ich ein altes Thema wieder einmal aufgreife. Denke mal Copy Constructoren sind hier schon sehr oft behandelt worden, hab aber trotzdem eine Frage, bei der ich hilfe brauche!</p>
<p>Also, folgendes (binge hier nur den noetigen Quellcode - also die Copy Constructoren - hoffe dar reicht, sonst reich ich den Rest noch nach):</p>
<pre><code class="language-cpp">Node::Node(const Node &amp;node) :
	left(0), right(0) {
	if (node.left != 0)
		this-&gt;left = new Node(*(node.left));
	if (node.right != 0)
		this-&gt;right = new Node(*(node.right));
}
</code></pre>
<p>und die abgeleitete Klasse (public):</p>
<pre><code class="language-cpp">FSNode::FSNode(const FSNode &amp;fsnode) :
	Node(fsnode), name(fsnode.name) {
}
</code></pre>
<p>Problemstellung:<br />
left und right sind Elemente vom Typ Node, das heisst es koennen auch abgeleitete Klassen dort verwendet werden - sprich FSNode.<br />
Wenn ich jetzt folgendes mache:</p>
<pre><code class="language-cpp">FSNode *fsn1 = new FSNode(&quot;FSNode #1&quot;);
	fsn1-&gt;SetLeft(new FSNode(&quot;FSNode left&quot;));
	fsn1-&gt;SetRight(new FSNode(&quot;FSNode right&quot;));

	FSNode *fsn2 = new FSNode(*fsn1);
	// delete spar ich mir hier auf
</code></pre>
<p>sind bei fsn1 left und right zwar laut dynamischer Bindung Objekte vom Typ<br />
FSNode, dies wird aber vom Kopierkonstructor nicht beruecksichtigt und ich erhalte bei fsn2 nur Elemente vom Typ Node.<br />
Warum das so ist mir klar, hab auch das Konzept dahinter verstanden (jedenfalls ohne Ableitung).<br />
Aber ich habe keinen wirklichen Plan, wie man das hier loesen soll ...<br />
Von FSNode leiten naehmlich noch mal zwei Klassen ab, das ganze ist also relativ kompliziert schon ....</p>
<p>Ich hoffe natuerlich wie immer dass die Problemerklaerung halbwegs verstaendlich ist, falls es noch Erlaeuterungen braucht nur melden.<br />
Aber ich bin mir sicher viele von euch kennen das Problem und koennen mir auch demensprechend weiterhelfen!</p>
<p>Danke schon mal im voraus!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/256358/copy-constructor-vererbung-amp-dynamische-bindung</link><generator>RSS for Node</generator><lastBuildDate>Fri, 11 Sep 2026 09:01:53 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/256358.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 11 Dec 2009 15:43:30 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Copy Constructor: Vererbung &amp;amp; dynamische Bindung on Fri, 11 Dec 2009 15:43:30 GMT]]></title><description><![CDATA[<p>Tut mir leid, dass ich ein altes Thema wieder einmal aufgreife. Denke mal Copy Constructoren sind hier schon sehr oft behandelt worden, hab aber trotzdem eine Frage, bei der ich hilfe brauche!</p>
<p>Also, folgendes (binge hier nur den noetigen Quellcode - also die Copy Constructoren - hoffe dar reicht, sonst reich ich den Rest noch nach):</p>
<pre><code class="language-cpp">Node::Node(const Node &amp;node) :
	left(0), right(0) {
	if (node.left != 0)
		this-&gt;left = new Node(*(node.left));
	if (node.right != 0)
		this-&gt;right = new Node(*(node.right));
}
</code></pre>
<p>und die abgeleitete Klasse (public):</p>
<pre><code class="language-cpp">FSNode::FSNode(const FSNode &amp;fsnode) :
	Node(fsnode), name(fsnode.name) {
}
</code></pre>
<p>Problemstellung:<br />
left und right sind Elemente vom Typ Node, das heisst es koennen auch abgeleitete Klassen dort verwendet werden - sprich FSNode.<br />
Wenn ich jetzt folgendes mache:</p>
<pre><code class="language-cpp">FSNode *fsn1 = new FSNode(&quot;FSNode #1&quot;);
	fsn1-&gt;SetLeft(new FSNode(&quot;FSNode left&quot;));
	fsn1-&gt;SetRight(new FSNode(&quot;FSNode right&quot;));

	FSNode *fsn2 = new FSNode(*fsn1);
	// delete spar ich mir hier auf
</code></pre>
<p>sind bei fsn1 left und right zwar laut dynamischer Bindung Objekte vom Typ<br />
FSNode, dies wird aber vom Kopierkonstructor nicht beruecksichtigt und ich erhalte bei fsn2 nur Elemente vom Typ Node.<br />
Warum das so ist mir klar, hab auch das Konzept dahinter verstanden (jedenfalls ohne Ableitung).<br />
Aber ich habe keinen wirklichen Plan, wie man das hier loesen soll ...<br />
Von FSNode leiten naehmlich noch mal zwei Klassen ab, das ganze ist also relativ kompliziert schon ....</p>
<p>Ich hoffe natuerlich wie immer dass die Problemerklaerung halbwegs verstaendlich ist, falls es noch Erlaeuterungen braucht nur melden.<br />
Aber ich bin mir sicher viele von euch kennen das Problem und koennen mir auch demensprechend weiterhelfen!</p>
<p>Danke schon mal im voraus!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1821255</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1821255</guid><dc:creator><![CDATA[moe szyslak]]></dc:creator><pubDate>Fri, 11 Dec 2009 15:43:30 GMT</pubDate></item><item><title><![CDATA[Reply to Copy Constructor: Vererbung &amp;amp; dynamische Bindung on Fri, 11 Dec 2009 16:04:18 GMT]]></title><description><![CDATA[<p>Da wirst du wohl eine virtuelle clone() Methode implementieren müssen:</p>
<pre><code class="language-cpp">class Node
{
public;
   virtual ~Node();
   virtual Node* clone() const = 0;
};

class FSNode
{
public;
   Node* clone() const
   {
      return new FSNode( *this );
   }
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1821264</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1821264</guid><dc:creator><![CDATA[DocShoe]]></dc:creator><pubDate>Fri, 11 Dec 2009 16:04:18 GMT</pubDate></item><item><title><![CDATA[Reply to Copy Constructor: Vererbung &amp;amp; dynamische Bindung on Fri, 11 Dec 2009 16:07:56 GMT]]></title><description><![CDATA[<p>moe szyslak schrieb:</p>
<blockquote>
<p>Wenn ich jetzt folgendes mache:</p>
<pre><code class="language-cpp">FSNode *fsn1 = new FSNode(&quot;FSNode #1&quot;);
	fsn1-&gt;SetLeft(new FSNode(&quot;FSNode left&quot;));
	fsn1-&gt;SetRight(new FSNode(&quot;FSNode right&quot;));

	FSNode *fsn2 = new FSNode(*fsn1);
	// delete spar ich mir hier auf
</code></pre>
<p>sind bei fsn1 left und right zwar laut dynamischer Bindung Objekte vom Typ<br />
FSNode, dies wird aber vom Kopierkonstructor nicht beruecksichtigt und ich erhalte bei fsn2 nur Elemente vom Typ Node.</p>
</blockquote>
<p>Das dürfte eigentlich nicht so sein, da du nur statische Typen verwendest. <code>fsn1</code> und <code>fsn2</code> sind Zeiger auf <code>FSNode</code> . Wieso meinst du, dass du nur Elemente des Typs <code>Node</code> erhältst?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1821267</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1821267</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Fri, 11 Dec 2009 16:07:56 GMT</pubDate></item><item><title><![CDATA[Reply to Copy Constructor: Vererbung &amp;amp; dynamische Bindung on Fri, 11 Dec 2009 16:18:45 GMT]]></title><description><![CDATA[<p>Nexus schrieb:</p>
<blockquote>
<p>moe szyslak schrieb:</p>
<blockquote>
<p>Wenn ich jetzt folgendes mache:</p>
<pre><code class="language-cpp">FSNode *fsn1 = new FSNode(&quot;FSNode #1&quot;);
	fsn1-&gt;SetLeft(new FSNode(&quot;FSNode left&quot;));
	fsn1-&gt;SetRight(new FSNode(&quot;FSNode right&quot;));

	FSNode *fsn2 = new FSNode(*fsn1);
	// delete spar ich mir hier auf
</code></pre>
<p>sind bei fsn1 left und right zwar laut dynamischer Bindung Objekte vom Typ<br />
FSNode, dies wird aber vom Kopierkonstructor nicht beruecksichtigt und ich erhalte bei fsn2 nur Elemente vom Typ Node.</p>
</blockquote>
<p>Das dürfte eigentlich nicht so sein, da du nur statische Typen verwendest. <code>fsn1</code> und <code>fsn2</code> sind Zeiger auf <code>FSNode</code> . Wieso meinst du, dass du nur Elemente des Typs <code>Node</code> erhältst?</p>
</blockquote>
<p>Naja, Node definiert und implementiert die Methode</p>
<pre><code class="language-cpp">void PrintNode(const int space, std::ostream &amp;out = std::cout) const;
</code></pre>
<p>und FSNode macht das auch und ich erhalte ff. Ergebnis:</p>
<p>FSNode #1 = FSNode #1<br />
FSNode #1 left = FSNode left<br />
FSNode #1 right = FSNode right<br />
FSNode #2 = FSNode #1<br />
FSNode #2 left = Node::PrintNode<br />
FSNode #2 right = Node::PrintNode</p>
<p>wenn ich ff. ausfuehre:</p>
<pre><code class="language-cpp">FSNode *fsn1 = new FSNode(&quot;FSNode #1&quot;);
	fsn1-&gt;SetLeft(new FSNode(&quot;FSNode left&quot;));
	fsn1-&gt;SetRight(new FSNode(&quot;FSNode right&quot;));

	FSNode *fsn2 = new FSNode(*fsn1);

	cout &lt;&lt; &quot;FSNode #1 = &quot; &lt;&lt; *fsn1 &lt;&lt; endl;
	cout &lt;&lt; &quot;FSNode #1 left = &quot; &lt;&lt; *fsn1-&gt;GetLeft() &lt;&lt; endl;
	cout &lt;&lt; &quot;FSNode #1 right = &quot; &lt;&lt; *fsn1-&gt;GetRight() &lt;&lt; endl;

	cout &lt;&lt; &quot;FSNode #2 = &quot; &lt;&lt; *fsn2 &lt;&lt; endl;
	cout &lt;&lt; &quot;FSNode #2 left = &quot; &lt;&lt; *fsn2-&gt;GetLeft() &lt;&lt; endl;
	cout &lt;&lt; &quot;FSNode #2 right = &quot; &lt;&lt; *fsn2-&gt;GetRight() &lt;&lt; endl;

	delete fsn2;
	delete fsn1;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1821271</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1821271</guid><dc:creator><![CDATA[moe szyslak]]></dc:creator><pubDate>Fri, 11 Dec 2009 16:18:45 GMT</pubDate></item><item><title><![CDATA[Reply to Copy Constructor: Vererbung &amp;amp; dynamische Bindung on Fri, 11 Dec 2009 16:24:00 GMT]]></title><description><![CDATA[<p>DocShoe schrieb:</p>
<blockquote>
<p>Da wirst du wohl eine virtuelle clone() Methode implementieren müssen:</p>
<pre><code class="language-cpp">class Node
{
public;
   virtual ~Node();
   virtual Node* clone() const = 0;
};

class FSNode
{
public;
   Node* clone() const
   {
      return new FSNode( *this );
   }
};
</code></pre>
</blockquote>
<p>Wenn ich das mache, dann wird Node ja eine abstrakte Klasse, die sollte aber nicht der Fall sein.<br />
Und wenn ich die Methoden im Node und FSNode implementiere (also ohne = 0) dann funktioniert es auch nicht!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1821275</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1821275</guid><dc:creator><![CDATA[moe szyslak]]></dc:creator><pubDate>Fri, 11 Dec 2009 16:24:00 GMT</pubDate></item><item><title><![CDATA[Reply to Copy Constructor: Vererbung &amp;amp; dynamische Bindung on Fri, 11 Dec 2009 16:27:36 GMT]]></title><description><![CDATA[<p>Mach <code>PrintNode()</code> virtuell und schau nochmals.</p>
<p>Nimm kein <code>clone()</code> , wenn es nicht absolut notwendig ist. Das ist nur ein Ansatz, um Kopiersemantik im polymorphen Kontext überhaupt zu ermöglichen. Solange du auf einer ebene direkten Zugriff auf die statischen Typen hast, implementierst du Kopien lieber über den Kopierkonstruktor.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1821280</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1821280</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Fri, 11 Dec 2009 16:27:36 GMT</pubDate></item><item><title><![CDATA[Reply to Copy Constructor: Vererbung &amp;amp; dynamische Bindung on Fri, 11 Dec 2009 16:29:51 GMT]]></title><description><![CDATA[<p>moe szyslak schrieb:</p>
<blockquote>
<p>dann funktioniert es auch nicht!</p>
</blockquote>
<p>Doch, es funktioniert.</p>
<p>Da du allerdings nicht sagst warum du meinst dass es nicht funktioniert (Fehlermeldung?), sag ich jetzt auch nicht warum ich glaube dass es funktioniert und wie es funktioniert.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1821283</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1821283</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Fri, 11 Dec 2009 16:29:51 GMT</pubDate></item><item><title><![CDATA[Reply to Copy Constructor: Vererbung &amp;amp; dynamische Bindung on Fri, 11 Dec 2009 16:37:07 GMT]]></title><description><![CDATA[<p>OK, also doch der ganze Code:<br />
Node.h</p>
<pre><code class="language-cpp">#ifndef NODE_H_
#define NODE_H_

#include &lt;iostream&gt;

class Node {
	friend class FSNode;

private:
	Node *left, *right;

	friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;out, const Node &amp;node);

protected:
	virtual void PrintNode(const int space, std::ostream &amp;out = std::cout) const;

public:
	Node(Node *l = 0, Node *r = 0);
	Node(const Node &amp;node);
	virtual Node* clone() const;
	virtual ~Node();

	virtual void SetLeft(Node *l);
	virtual Node *GetLeft() const;
	virtual void SetRight(Node *r);
	virtual Node *GetRight() const;

	virtual Node &amp;operator=(const Node &amp;node);
};

std::ostream &amp;operator&lt;&lt;(std::ostream &amp;out, const Node &amp;node);

#endif /* NODE_H_ */
</code></pre>
<p>Node.cpp</p>
<pre><code class="language-cpp">#include &quot;Node.h&quot;
#include &lt;iomanip&gt;
using namespace std;

Node::Node(Node *l, Node *r) :
	left(l), right(r) {
}

Node::Node(const Node &amp;node) :
	left(0), right(0) {
	if (node.left != 0) {
		this-&gt;left = new Node(*(node.left));
	}
	if (node.right != 0) {
		this-&gt;right = new Node(*(node.right));
	}
}

Node* Node::clone() const {
	return new Node(*this);
}

Node::~Node() {
	if (this-&gt;left != 0)
		delete this-&gt;left;
	if (this-&gt;right != 0)
		delete this-&gt;right;
}

Node *Node::GetLeft() const {
	return this-&gt;left;
}

void Node::SetRight(Node *r) {
	this-&gt;right = r;
}

Node *Node::GetRight() const {
	return this-&gt;right;
}

void Node::SetLeft(Node *l) {
	this-&gt;left = l;
}

void Node::PrintNode(const int space, std::ostream &amp;out) const {
	out &lt;&lt; &quot;Node::PrintNode &quot;;
}

Node &amp;Node::operator=(const Node &amp;node) {
	if (this == &amp;node)
		return *this;

	if (this-&gt;left != 0)
		delete this-&gt;left;
	if (this-&gt;right != 0)
		delete this-&gt;right;

	if (node.left != 0) {
		this-&gt;left = new Node(*(node.left));
	}
	if (node.right != 0) {
		this-&gt;right = new Node(*(node.right));
	}

	return *this;
}

std::ostream &amp;operator&lt;&lt;(std::ostream &amp;out, const Node &amp;node) {
	node.PrintNode(2, out);
	return out &lt;&lt; flush;
}
</code></pre>
<p>FSNode.h</p>
<pre><code class="language-cpp">#ifndef FSNODE_H_
#define FSNODE_H_

#include &quot;Node.h&quot;

#include &lt;string&gt;

class FSNode: public Node {
protected:
	std::string name;
	virtual void PrintNode(const int space, std::ostream &amp;out = std::cout) const;

public:
	FSNode(std::string name);
	FSNode(const FSNode &amp;fsnode);
	virtual Node* clone() const;
	virtual ~FSNode();

	void AddRight(FSNode *node);
	std::string getName() const;
};

#endif /* FSNODE_H_ */
</code></pre>
<p>FSNode.cpp</p>
<pre><code class="language-cpp">FSNode::FSNode(std::string name) :
	Node(0, 0), name(name) {
}

FSNode::FSNode(const FSNode &amp;fsnode) :
	Node(fsnode), name(fsnode.name) {
}

Node* FSNode::clone() const {
	return new FSNode(*this);
}

FSNode::~FSNode() {
}

void FSNode::PrintNode(const int space, std::ostream &amp;out) const {
	out &lt;&lt; this-&gt;name;
}

void FSNode::AddRight(FSNode *node) {
	if (this-&gt;GetRight() == 0) {
		this-&gt;SetRight(node);
	} else {
		Node *tmp = this-&gt;GetRight(), *prev = 0;
		while (tmp != 0) {
			prev = tmp;
			tmp = tmp-&gt;GetRight();
		}
		prev-&gt;SetRight(node);
	}
}

string FSNode::getName() const {
	return this-&gt;name;
}
</code></pre>
<p>und eben mit dieser Implementierung und dem Test der oben beschrieben ist funktioniert es nicht.<br />
Hilft das weiter? Ich denke mal ich hab da was doch nicht verstanden wenn ihr sagt, dass es eigentlich gehen sollte ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1821286</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1821286</guid><dc:creator><![CDATA[moe szyslak]]></dc:creator><pubDate>Fri, 11 Dec 2009 16:37:07 GMT</pubDate></item><item><title><![CDATA[Reply to Copy Constructor: Vererbung &amp;amp; dynamische Bindung on Fri, 11 Dec 2009 16:47:19 GMT]]></title><description><![CDATA[<p>Ah, hab's mir doch gedacht, die Implementierung der Clone Methode ist falsch!<br />
Hab jetzt ein bisschen gegoogelt und hab die richtige Loesung!</p>
<p>Danke fuer die Schnelle Antwort!</p>
<p>P.S. natuerlich werd ich jetzt den Zuweisungsoperator auch noch anpassen muessen</p>
<p>P.P.S. noch mal danke</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1821295</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1821295</guid><dc:creator><![CDATA[moe szyslak]]></dc:creator><pubDate>Fri, 11 Dec 2009 16:47:19 GMT</pubDate></item><item><title><![CDATA[Reply to Copy Constructor: Vererbung &amp;amp; dynamische Bindung on Fri, 11 Dec 2009 16:47:42 GMT]]></title><description><![CDATA[<p>Du brauchst doch eine <code>clone()</code> -Funktion, weil du polymorph kopierst. Ich habe zuerst gar nicht daran gedacht, dass die <code>Node</code> -Klasse im Kopierkonstruktor ja ihrerseits tiefe, polymorphe Kopien durchführen muss.</p>
<p>Verwende nächstes Mal einen Debugger. Da kannst du sehr gut sehen, von welchem dynamischen Typ ein Objekt ist. Zur Not tuts auch RTTI direkt im Code.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1821296</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1821296</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Fri, 11 Dec 2009 16:47:42 GMT</pubDate></item></channel></rss>