<?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[Problem mit virtual und polymorphi]]></title><description><![CDATA[<p>Hi,<br />
folgendes Prob:<br />
In ner Basisklasse (Widget) ist ne virutelle Methode deffiniert (getParentWorld()). In der Abgeleiteten Klasse (World) ist sie ueberschrieben.</p>
<p>Wenn ich jetzt im Hauptprogramm (main()) einen Widget* pointer auf ein World objekt habe und ueber den pointer-&gt;getParentWorld() von der World aufrufen will, wird die Methode vom Widget und nicht von der World ausgefuehrt.</p>
<p>Ich hoffe, dass das verstaendlich ist, hier noch der Code:</p>
<p>main.cpp</p>
<pre><code class="language-cpp">#include &quot;widget.h&quot;
#include &quot;world.h&quot;

#include &lt;iostream&gt;
using std::cerr;
using std::endl;

using calina::Widget;
using calina::World;

int main() {
	World p(&quot;world&quot;);
	Widget ch1(&amp;p, &quot;ch1&quot;);

	ch1.getParentWorld();

	cerr &lt;&lt; &quot;=============================&quot; &lt;&lt; endl;

	Widget* test = &amp;p;
	test-&gt;getParentWorld();

	return 0;
/*
Ausgabe:
ch1=&gt; Widget::getParentWorld()  parentWidget=0xbff81dbc
world=&gt; Widget::getParentWorld()        parentWidget=0
=============================
world=&gt; Widget::getParentWorld()        parentWidget=0

Gewuenschte Ausgabe:
ch1=&gt; Widget::getParentWorld()  parentWidget=0xbff81dbc
world=&gt; Widget::getParentWorld()        this=0xbf964fa8
=============================
world=&gt; Widget::getParentWorld()        this=0xbf964fa8
*/
}
</code></pre>
<p>widget.h</p>
<pre><code class="language-cpp">#ifndef CALINAWIDGET_H
#define CALINAWIDGET_H

#include &lt;QObject&gt;
#include &lt;QString&gt;

namespace calina {
class World;
class Widget : public QObject {
	Q_OBJECT
public:
	Widget(Widget *parent, QString name=&quot;&quot;);
	virtual ~Widget();
	QString getName() const;
	void setName(QString name);
	virtual World* getParentWorld() const;
	Widget* getParent() const;
private:
};
}

#endif
</code></pre>
<p>world.h</p>
<pre><code class="language-cpp">#ifndef CALINAWORLD_H
#define CALINAWORLD_H

#include &quot;widget.h&quot;

class QGLWidget;

namespace calina {

class World : public virtual Widget {
public:
	World(QString name=&quot;World&quot;);
	~World();
	virtual World* getParentWorld();
};

}

#endif
</code></pre>
<p>world.cpp</p>
<pre><code class="language-cpp">#include &quot;world.h&quot;

#include &lt;iostream&gt;
using std::cerr;
using std::endl;

namespace calina {

World::World(QString name)
	: Widget(0, name) {
}

World::~World() {
}

World* World::getParentWorld() {
	cerr &lt;&lt;  getName().toStdString() &lt;&lt;
			&quot;=&gt; Widget::getParentWorld()\tthis=&quot; &lt;&lt; this &lt;&lt; endl;
	return this;
}

}
</code></pre>
<p>Ich verwende die Qt Library, hier die Dokumentation zu QObject:<br />
<a href="http://doc.trolltech.com/4.2/qobject.html" rel="nofollow">http://doc.trolltech.com/4.2/qobject.html</a></p>
<p>Ich hab da relativ lang gesucht, aber den Bock nicht gefunden. Ist sicher etwas ganz einfaches..</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/187059/problem-mit-virtual-und-polymorphi</link><generator>RSS for Node</generator><lastBuildDate>Thu, 24 Sep 2026 13:57:04 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/187059.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 15 Jul 2007 11:29:07 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Problem mit virtual und polymorphi on Sun, 15 Jul 2007 11:30:36 GMT]]></title><description><![CDATA[<p>Hi,<br />
folgendes Prob:<br />
In ner Basisklasse (Widget) ist ne virutelle Methode deffiniert (getParentWorld()). In der Abgeleiteten Klasse (World) ist sie ueberschrieben.</p>
<p>Wenn ich jetzt im Hauptprogramm (main()) einen Widget* pointer auf ein World objekt habe und ueber den pointer-&gt;getParentWorld() von der World aufrufen will, wird die Methode vom Widget und nicht von der World ausgefuehrt.</p>
<p>Ich hoffe, dass das verstaendlich ist, hier noch der Code:</p>
<p>main.cpp</p>
<pre><code class="language-cpp">#include &quot;widget.h&quot;
#include &quot;world.h&quot;

#include &lt;iostream&gt;
using std::cerr;
using std::endl;

using calina::Widget;
using calina::World;

int main() {
	World p(&quot;world&quot;);
	Widget ch1(&amp;p, &quot;ch1&quot;);

	ch1.getParentWorld();

	cerr &lt;&lt; &quot;=============================&quot; &lt;&lt; endl;

	Widget* test = &amp;p;
	test-&gt;getParentWorld();

	return 0;
/*
Ausgabe:
ch1=&gt; Widget::getParentWorld()  parentWidget=0xbff81dbc
world=&gt; Widget::getParentWorld()        parentWidget=0
=============================
world=&gt; Widget::getParentWorld()        parentWidget=0

Gewuenschte Ausgabe:
ch1=&gt; Widget::getParentWorld()  parentWidget=0xbff81dbc
world=&gt; Widget::getParentWorld()        this=0xbf964fa8
=============================
world=&gt; Widget::getParentWorld()        this=0xbf964fa8
*/
}
</code></pre>
<p>widget.h</p>
<pre><code class="language-cpp">#ifndef CALINAWIDGET_H
#define CALINAWIDGET_H

#include &lt;QObject&gt;
#include &lt;QString&gt;

namespace calina {
class World;
class Widget : public QObject {
	Q_OBJECT
public:
	Widget(Widget *parent, QString name=&quot;&quot;);
	virtual ~Widget();
	QString getName() const;
	void setName(QString name);
	virtual World* getParentWorld() const;
	Widget* getParent() const;
private:
};
}

#endif
</code></pre>
<p>world.h</p>
<pre><code class="language-cpp">#ifndef CALINAWORLD_H
#define CALINAWORLD_H

#include &quot;widget.h&quot;

class QGLWidget;

namespace calina {

class World : public virtual Widget {
public:
	World(QString name=&quot;World&quot;);
	~World();
	virtual World* getParentWorld();
};

}

#endif
</code></pre>
<p>world.cpp</p>
<pre><code class="language-cpp">#include &quot;world.h&quot;

#include &lt;iostream&gt;
using std::cerr;
using std::endl;

namespace calina {

World::World(QString name)
	: Widget(0, name) {
}

World::~World() {
}

World* World::getParentWorld() {
	cerr &lt;&lt;  getName().toStdString() &lt;&lt;
			&quot;=&gt; Widget::getParentWorld()\tthis=&quot; &lt;&lt; this &lt;&lt; endl;
	return this;
}

}
</code></pre>
<p>Ich verwende die Qt Library, hier die Dokumentation zu QObject:<br />
<a href="http://doc.trolltech.com/4.2/qobject.html" rel="nofollow">http://doc.trolltech.com/4.2/qobject.html</a></p>
<p>Ich hab da relativ lang gesucht, aber den Bock nicht gefunden. Ist sicher etwas ganz einfaches..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1325766</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1325766</guid><dc:creator><![CDATA[aMan]]></dc:creator><pubDate>Sun, 15 Jul 2007 11:30:36 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit virtual und polymorphi on Sun, 15 Jul 2007 11:43:49 GMT]]></title><description><![CDATA[<p>hoi</p>
<pre><code class="language-cpp">virtual World* getParentWorld() const; // &lt;- const vergessen ?
</code></pre>
<p>Meep Meep</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1325776</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1325776</guid><dc:creator><![CDATA[Meep Meep]]></dc:creator><pubDate>Sun, 15 Jul 2007 11:43:49 GMT</pubDate></item><item><title><![CDATA[Reply to Problem mit virtual und polymorphi on Sun, 15 Jul 2007 12:55:59 GMT]]></title><description><![CDATA[<p>Danke, das wars..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1325831</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1325831</guid><dc:creator><![CDATA[aMan]]></dc:creator><pubDate>Sun, 15 Jul 2007 12:55:59 GMT</pubDate></item></channel></rss>