<?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[typenloses Array?]]></title><description><![CDATA[<p>Hallo!</p>
<p>Ich habe eine Klassenstruktur, bestehend aus einer Superklasse und zwei davon abgeleteten Klassen (A &amp; B).</p>
<p>Jetzt würde ich gerne ein Array erstellen, welches Elemente sowohl von A als auch von B enthalten kann.</p>
<p>Wie mache ich das?</p>
<p>Grüße, pktm</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/203800/typenloses-array</link><generator>RSS for Node</generator><lastBuildDate>Wed, 07 Oct 2026 22:02:09 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/203800.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 26 Jan 2008 17:10:27 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to typenloses Array? on Sat, 26 Jan 2008 17:10:27 GMT]]></title><description><![CDATA[<p>Hallo!</p>
<p>Ich habe eine Klassenstruktur, bestehend aus einer Superklasse und zwei davon abgeleteten Klassen (A &amp; B).</p>
<p>Jetzt würde ich gerne ein Array erstellen, welches Elemente sowohl von A als auch von B enthalten kann.</p>
<p>Wie mache ich das?</p>
<p>Grüße, pktm</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1444102</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1444102</guid><dc:creator><![CDATA[pktm]]></dc:creator><pubDate>Sat, 26 Jan 2008 17:10:27 GMT</pubDate></item><item><title><![CDATA[Reply to typenloses Array? on Sat, 26 Jan 2008 17:14:20 GMT]]></title><description><![CDATA[<p>std::vector&lt;Superklasse*&gt; oder Superklasse * foo = new Superklasse*[N];</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1444105</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1444105</guid><dc:creator><![CDATA[this-*gt*that]]></dc:creator><pubDate>Sat, 26 Jan 2008 17:14:20 GMT</pubDate></item><item><title><![CDATA[Reply to typenloses Array? on Sat, 26 Jan 2008 17:20:58 GMT]]></title><description><![CDATA[<p>Hi, wie wärs, wenn du ein Array mit Zeigern auf die Superklasse erstellst und die Methoden als virtuell deklarierst?</p>
<p>Dann könntest du die Elemente in das Array einfügen und wenn du auf sie zugreifst wird zur Laufzeit entschieden, ob es sich um Klasse A oder Klasse B handelt.</p>
<p>Viele Grüße<br />
Andreas</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1444107</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1444107</guid><dc:creator><![CDATA[Andreas_LL]]></dc:creator><pubDate>Sat, 26 Jan 2008 17:20:58 GMT</pubDate></item><item><title><![CDATA[Reply to typenloses Array? on Sat, 26 Jan 2008 17:26:18 GMT]]></title><description><![CDATA[<p>Cool, das hilft mir schonmal weiter.</p>
<p>Bei der Verwendung des Datentype der Superklasse hatte ich das Problem, dass ich die Eigenschaften der Unterklassen verlor, wenn ich die Objekte in dieses Array aufnahm.<br />
Beispiel:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;

int main()
{

	class Test
	{
		public:
			string name;
	};

	class MyTest : public Test
	{
		public: 
			int zahl;

	};

	Test array[2];

	MyTest olaf;

	olaf.name = &quot;Peter&quot;;
	olaf.zahl = 5;

	Test irina;
	irina.name = &quot;hans&quot;;

	array[0] = olaf;

	cout &lt;&lt; array[0].name &lt;&lt; &quot; &quot; &lt;&lt; array[0].zahl &lt;&lt; &quot;\n&quot;;

	system(&quot;pause&quot;);

}
</code></pre>
<p>Grüße, pktm</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1444109</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1444109</guid><dc:creator><![CDATA[pktm]]></dc:creator><pubDate>Sat, 26 Jan 2008 17:26:18 GMT</pubDate></item><item><title><![CDATA[Reply to typenloses Array? on Sat, 26 Jan 2008 17:37:34 GMT]]></title><description><![CDATA[<p>Darum solltest du auch Zeiger auf die eigentlichen Objekte speichern, siehe this-&gt;thats post.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1444114</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1444114</guid><dc:creator><![CDATA[DeSoVoDaMu]]></dc:creator><pubDate>Sat, 26 Jan 2008 17:37:34 GMT</pubDate></item><item><title><![CDATA[Reply to typenloses Array? on Sat, 26 Jan 2008 17:42:38 GMT]]></title><description><![CDATA[<p>Versuch mal folgendes:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt; 
#include &lt;string&gt; 
using namespace std; 

int main() 
{ 
    class Test 
    { 
        public: 
            virtual void SetName(string name){};
            virtual string GetName(){};
            string m_sname; 
    }; 

    class MyTest : public Test 
    { 
        public: 
            void SetName(string name){m_sname = name};
            string GetName(){return m_sname;}
            int zahl; 
    }; 

    Test* array[2]; 

    MyTest* olaf = new MyTest; 

    olaf-&gt;SetName(string(&quot;Peter&quot;); 
   // olaf.zahl = 5; 

    MyTest* irina = new MyTest; 
    irina-&gt;SetName(&quot;hans&quot;); 

    array[0] = olaf; 
    array[1] = irina; 

    cout &lt;&lt; array[0]-&gt;GetName() &lt;&lt; &quot; &quot; &lt;&lt; array[1]-&gt;GetName() &lt;&lt; &quot;\n&quot;; 

    system(&quot;pause&quot;);
}
</code></pre>
<p>Ich habs jetzt aber nicht ausprobiert, kann sein das kleinere fehler drin sind <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>
]]></description><link>https://www.c-plusplus.net/forum/post/1444119</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1444119</guid><dc:creator><![CDATA[Andreas_LL]]></dc:creator><pubDate>Sat, 26 Jan 2008 17:42:38 GMT</pubDate></item><item><title><![CDATA[Reply to typenloses Array? on Sat, 26 Jan 2008 17:48:31 GMT]]></title><description><![CDATA[<p>So funktionierts aber^^</p>
<pre><code class="language-cpp">#include &lt;iostream&gt; 
#include &lt;string&gt; 
using namespace std; 

int main() 
{ 
    class Test 
    { 
        string m_sname;     
		public: 
            virtual void SetName(string name){}; 
            virtual string GetName(){return &quot;&quot;;}; 

    }; 

    class MyTest : public Test 
    { 
        public: 
            void SetName(string name){m_sname = name;} 
            string GetName(){return m_sname;} 
            int zahl; 
    }; 

    Test* array[2]; 

    MyTest* olaf = new MyTest; 

    olaf-&gt;SetName(string(&quot;Peter&quot;)); 
   // olaf.zahl = 5; 

    MyTest* irina = new MyTest; 
    irina-&gt;SetName(string(&quot;hans&quot;)); 

    array[0] = olaf; 
    array[1] = irina; 

    cout &lt;&lt; array[0]-&gt;GetName() &lt;&lt; &quot; &quot; &lt;&lt; array[1]-&gt;GetName() &lt;&lt; &quot;\n&quot;; 

    system(&quot;pause&quot;); 
	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1444122</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1444122</guid><dc:creator><![CDATA[Andreas_LL]]></dc:creator><pubDate>Sat, 26 Jan 2008 17:48:31 GMT</pubDate></item><item><title><![CDATA[Reply to typenloses Array? on Sat, 26 Jan 2008 17:54:37 GMT]]></title><description><![CDATA[<p>Hm, bei mir kommt da:</p>
<p>error C2248: &quot;main::Test::m_sname&quot;: Kein Zugriff auf private Member, dessen Deklaration in der main::Test-Klasse erfolgte.</p>
<p>:-S</p>
<p>Ich hab hier Visual C++ Express Edition 2005 (Version 8.0).</p>
<p>So viel Ahnung hab ich von C++ leider noch nicht, hoffe ihr könnt mir noch ein bißchen helfen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1444125</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1444125</guid><dc:creator><![CDATA[pktm]]></dc:creator><pubDate>Sat, 26 Jan 2008 17:54:37 GMT</pubDate></item><item><title><![CDATA[Reply to typenloses Array? on Sat, 26 Jan 2008 18:00:10 GMT]]></title><description><![CDATA[<p>Zum Testen:</p>
<p>Erweitert um Methoden für Zahl.</p>
<pre><code>#include &lt;iostream&gt; 
#include &lt;string&gt; 
using namespace std; 

int main() 
{ 
    class Test 
    { 
		string m_sname;
        public: 
            virtual void SetName(string name){}; 
            virtual string GetName(){return &quot;&quot;;}; 

    }; 

    class MyTest : public Test 
    { 
		private:
			int m_zahl;
        public: 
            void SetName(string name){m_sname = name;} 
            string GetName(){return m_sname;} 
			void SetZahl(int z){m_zahl = z;} 
			int GetZahl(){return m_zahl;} 
    }; 

    Test* array[2]; 

    MyTest* olaf = new MyTest;

    olaf-&gt;SetName(string(&quot;Peter&quot;));
    olaf-&gt;SetZahl(5); 

    MyTest* irina = new MyTest; 
    irina-&gt;SetName(string(&quot;hans&quot;)); 

    array[0] = olaf; 
    array[1] = irina; 

    cout &lt;&lt; array[0]-&gt;GetName() &lt;&lt; &quot; &quot; &lt;&lt; array[1]-&gt;GetName() &lt;&lt; array[0]-&gt;GetZahl() &lt;&lt; &quot;\n&quot;; 

    system(&quot;pause&quot;); 
    return 0; 
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1444128</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1444128</guid><dc:creator><![CDATA[pktm]]></dc:creator><pubDate>Sat, 26 Jan 2008 18:00:10 GMT</pubDate></item><item><title><![CDATA[Reply to typenloses Array? on Sat, 26 Jan 2008 18:00:37 GMT]]></title><description><![CDATA[<p>ja sry das hatte ich noch übersehen, was passiert denn wenn du über die Deklaration von m_sname in Test &quot;protected:&quot; schreibst?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1444129</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1444129</guid><dc:creator><![CDATA[Andreas_LL]]></dc:creator><pubDate>Sat, 26 Jan 2008 18:00:37 GMT</pubDate></item><item><title><![CDATA[Reply to typenloses Array? on Sat, 26 Jan 2008 18:14:24 GMT]]></title><description><![CDATA[<p>Andreas_LL schrieb:</p>
<blockquote>
<p>ja sry das hatte ich noch übersehen, was passiert denn wenn du über die Deklaration von m_sname in Test &quot;protected:&quot; schreibst?</p>
</blockquote>
<p>Ja, damit geht es. Erklärst du mir warum?</p>
<p>Was zudem aber nicht geht ist der zugriff auf eine nicht in der Superklasse vorhandenen Methode. Die scheint wieder getilgt zu werden. Oder kann man irgendwie angeben, ob man ein Objekt eines bestimmten Typs interpolieren will? In Java geht sowas z.B. so: Typ variable = (Typ)variableVonAnderemTyp;</p>
<p>Hier der momentane Code:</p>
<pre><code>#include &lt;iostream&gt; 
#include &lt;string&gt; 
using namespace std; 

int main() 
{ 
    class Test 
    { 
		protected:
			string m_sname;
        public: 
            virtual void SetName(string name){}; 
            virtual string GetName(){return &quot;&quot;;}; 

    }; 

    class MyTest : public Test 
    { 
		protected:
			int m_zahl;
        public: 
            void SetName(string name){m_sname = name;} 
            string GetName(){return m_sname;} 
			void SetZahl(int z){m_zahl = z;} 
			int GetZahl(){return m_zahl;} 
    }; 

    Test* array[2]; 

    MyTest* olaf = new MyTest;

    olaf-&gt;SetName(string(&quot;Peter&quot;));
    olaf-&gt;SetZahl(5); 

    MyTest* irina = new MyTest; 
    irina-&gt;SetName(string(&quot;hans&quot;)); 

    array[0] = olaf; 
    array[1] = irina; 

    cout &lt;&lt; array[0]-&gt;GetName() &lt;&lt; &quot; &quot; &lt;&lt; array[1]-&gt;GetName() &lt;&lt; array[0]-&gt;GetZahl() &lt;&lt; &quot;\n&quot;; 

    system(&quot;pause&quot;); 
    return 0; 
}
</code></pre>
<p>Grüße, pktm</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1444134</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1444134</guid><dc:creator><![CDATA[pktm]]></dc:creator><pubDate>Sat, 26 Jan 2008 18:14:24 GMT</pubDate></item><item><title><![CDATA[Reply to typenloses Array? on Sat, 26 Jan 2008 18:25:50 GMT]]></title><description><![CDATA[<p>Hi,<br />
&quot;private:&quot; bedeutet das nur Methoden dieser Klasse auf die Eigenschaftsvariablen zugreifen können<br />
&quot;protected:&quot; Bei dieser Einstellung können auch die abgeleiteten Klassen und mit ihnen die Methoden auf die Membervariablen zugreifen.<br />
&quot;public:&quot; Jeder kann auf die Daten zugreifen.</p>
<p>Für die Methoden gilt das gleiche, deshalb konntest du ja auch auf die Methoden wie SetName, etc zugreifen, weil sie als public deklariert waren.</p>
<p>Um auf eine Methode oder Membervariable einer Abgleiteten Klasse zugreifen zu können kannst du schreiben:</p>
<p>MyTest* dummy = (MyTest*) array[0];<br />
dummy-&gt;GetZahl();</p>
<p>in diesem Fall:</p>
<p>cout &lt;&lt; array[0]-&gt;GetName() &lt;&lt; &quot; &quot; &lt;&lt; array[1]-&gt;GetName() &lt;&lt; ((MyTest*)array[0])-&gt;GetZahl() &lt;&lt; &quot;\n&quot;;</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1444139</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1444139</guid><dc:creator><![CDATA[Andreas_LL]]></dc:creator><pubDate>Sat, 26 Jan 2008 18:25:50 GMT</pubDate></item><item><title><![CDATA[Reply to typenloses Array? on Sat, 26 Jan 2008 19:23:42 GMT]]></title><description><![CDATA[<p>Danke vielmals, das funktiert!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1444178</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1444178</guid><dc:creator><![CDATA[pktm]]></dc:creator><pubDate>Sat, 26 Jan 2008 19:23:42 GMT</pubDate></item><item><title><![CDATA[Reply to typenloses Array? on Sat, 26 Jan 2008 23:05:17 GMT]]></title><description><![CDATA[<p>Igit. Das nennt man dann C++?</p>
<pre><code class="language-cpp">#include &lt;string&gt; // std::string
#include &lt;vector&gt; // std::vector
#include &lt;memory&gt; // std::auto_ptr
#include &lt;iostream&gt; // std::cout

class base
{
    std::string m_name;

public:
    base(std::string const&amp; name)
        : m_name(name)
    {}

public:
    std::string const&amp; get_name() const { return m_name; }
};

class derived : public base
{
    int m_data;

public:
    derived(int data, std::string const&amp; name)
        : base(name), m_data(data)
    {}

public:
    int get_data() const { return m_data; }
};

int main()
{
    // std::vector&lt; std::auto_ptr&lt;base&gt; &gt; data;
    std::vector&lt;base*&gt; data;
    data.push_back(new base(&quot;{- base instance -}&quot;));
    data.push_back(new derive(10, &quot;{- derived instance -}&quot;));

    std::cout &lt;&lt; &quot;#01: &quot; &lt;&lt; data[0]-&gt;get_name() 
              &lt;&lt; &quot;#02: &quot; &lt;&lt; data[1]-&gt;get_name()
              &lt;&lt; &quot;    - data := &quot; &lt;&lt; static_cast&lt;derived*&gt;(data[1])-&gt;get_data() &lt;&lt; std::endl;

    // bei std::auto_ptr nicht notwendig! Sonst Speicher freigeben!
    for (std::vector&lt;base*&gt;::iterator it(data.begin()); it != data.end(); ++it)
        delete (*it);
}
</code></pre>
<p>... so wäre es doch schöner, oder?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1444253</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1444253</guid><dc:creator><![CDATA[*D*Evil]]></dc:creator><pubDate>Sat, 26 Jan 2008 23:05:17 GMT</pubDate></item></channel></rss>