<?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[Variablen Datentyp zurückgeben]]></title><description><![CDATA[<p>Ich möchte mir eine Klasse schreiben, welche variable Datentypen verwaltet. Unter anderem für eine eigene Array Klasse. Folgenden Ansatz habe ich:</p>
<pre><code class="language-cpp">class Mixed
{
	private:
		int intVal;
		float floatVal;
		bool boolVal;
		char charVal;
		string strVal;

		char type;

	public:
		Mixed(int val) {
			this-&gt;intVal = val;
			this-&gt;type = 'i';
		}

		Mixed(float val) {
			this-&gt;floatVal = val;
			this-&gt;type = 'f';
		}

		Mixed(bool val) {
			this-&gt;boolVal = val;
			this-&gt;type = 'b';
		}

		Mixed(char val) {
			this-&gt;charVal = val;
			this-&gt;type = 'c';
		}

		Mixed(string val) {
			this-&gt;strVal = val;
			this-&gt;type = 's';
		}
};
</code></pre>
<p>Jetzt kann ich für alle Datentypen eine jeweilige Return Funktion schreiben und mit this-&gt;type auswählen, aber das geht bestimmt auch effektiver..</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/168224/variablen-datentyp-zurückgeben</link><generator>RSS for Node</generator><lastBuildDate>Wed, 16 Sep 2026 09:05:18 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/168224.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 19 Dec 2006 18:57:43 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Variablen Datentyp zurückgeben on Tue, 19 Dec 2006 18:57:43 GMT]]></title><description><![CDATA[<p>Ich möchte mir eine Klasse schreiben, welche variable Datentypen verwaltet. Unter anderem für eine eigene Array Klasse. Folgenden Ansatz habe ich:</p>
<pre><code class="language-cpp">class Mixed
{
	private:
		int intVal;
		float floatVal;
		bool boolVal;
		char charVal;
		string strVal;

		char type;

	public:
		Mixed(int val) {
			this-&gt;intVal = val;
			this-&gt;type = 'i';
		}

		Mixed(float val) {
			this-&gt;floatVal = val;
			this-&gt;type = 'f';
		}

		Mixed(bool val) {
			this-&gt;boolVal = val;
			this-&gt;type = 'b';
		}

		Mixed(char val) {
			this-&gt;charVal = val;
			this-&gt;type = 'c';
		}

		Mixed(string val) {
			this-&gt;strVal = val;
			this-&gt;type = 's';
		}
};
</code></pre>
<p>Jetzt kann ich für alle Datentypen eine jeweilige Return Funktion schreiben und mit this-&gt;type auswählen, aber das geht bestimmt auch effektiver..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1195041</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1195041</guid><dc:creator><![CDATA[Maurice Ch.]]></dc:creator><pubDate>Tue, 19 Dec 2006 18:57:43 GMT</pubDate></item><item><title><![CDATA[Reply to Variablen Datentyp zurückgeben on Tue, 19 Dec 2006 19:14:22 GMT]]></title><description><![CDATA[<p>hoi</p>
<p>vielleicht mit nem union</p>
<p>Meep Meep</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1195049</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1195049</guid><dc:creator><![CDATA[Meep Meep]]></dc:creator><pubDate>Tue, 19 Dec 2006 19:14:22 GMT</pubDate></item><item><title><![CDATA[Reply to Variablen Datentyp zurückgeben on Tue, 19 Dec 2006 19:15:16 GMT]]></title><description><![CDATA[<p>union dürfte mit std::string schwer werden.</p>
<p>@Meep, was war doch gleich die Frage ?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1195050</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1195050</guid><dc:creator><![CDATA[Knuddlbaer]]></dc:creator><pubDate>Tue, 19 Dec 2006 19:15:16 GMT</pubDate></item><item><title><![CDATA[Reply to Variablen Datentyp zurückgeben on Tue, 19 Dec 2006 19:20:33 GMT]]></title><description><![CDATA[<p>Templates?!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1195054</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1195054</guid><dc:creator><![CDATA[Konrad Rudolph]]></dc:creator><pubDate>Tue, 19 Dec 2006 19:20:33 GMT</pubDate></item><item><title><![CDATA[Reply to Variablen Datentyp zurückgeben on Tue, 19 Dec 2006 19:22:41 GMT]]></title><description><![CDATA[<p>Und wenn es zur Laufzeit sein muss verwende boost::any</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1195056</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1195056</guid><dc:creator><![CDATA[lolz]]></dc:creator><pubDate>Tue, 19 Dec 2006 19:22:41 GMT</pubDate></item><item><title><![CDATA[Reply to Variablen Datentyp zurückgeben on Tue, 19 Dec 2006 19:26:31 GMT]]></title><description><![CDATA[<p>Oder nen Pointer auf void?<br />
Dann müsste man halt ein struct draus machen<br />
wo auch noch drinsteht welcher Datentyp es sein soll.</p>
<p>Eine Union würde doch auch mit nem string gehn <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /> .</p>
<p>Aber eigentlich zuviel Aufwand und zuviel gefrickel.</p>
<p>Templates, wofür programmiert man sonst C++...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1195057</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1195057</guid><dc:creator><![CDATA[Mario Sandler]]></dc:creator><pubDate>Tue, 19 Dec 2006 19:26:31 GMT</pubDate></item><item><title><![CDATA[Reply to Variablen Datentyp zurückgeben on Tue, 19 Dec 2006 19:30:02 GMT]]></title><description><![CDATA[<p><a href="http://Storm.Xapek.de" rel="nofollow">Storm.Xapek.de</a> schrieb:</p>
<blockquote>
<p>Oder nen Pointer auf void?</p>
</blockquote>
<p>Nee, das wäre doch sehr unschön.</p>
<p><a href="http://Storm.Xapek.de" rel="nofollow">Storm.Xapek.de</a> schrieb:</p>
<blockquote>
<p>wo auch noch drinsteht welcher Datentyp es sein soll.</p>
</blockquote>
<p>Damit muss man sich aber zwangsweise auf wenige ausgewählte Typen beschränken.</p>
<p><a href="http://Storm.Xapek.de" rel="nofollow">Storm.Xapek.de</a> schrieb:</p>
<blockquote>
<p>Eine Union würde doch auch mit nem string gehn <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    />.</p>
</blockquote>
<p>Ne geht nicht.</p>
<p><a href="http://Storm.Xapek.de" rel="nofollow">Storm.Xapek.de</a> schrieb:</p>
<blockquote>
<p>Aber eigentlich zuviel Aufwand und zuviel gefrickel.</p>
</blockquote>
<p>ganz genau <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><a href="http://Storm.Xapek.de" rel="nofollow">Storm.Xapek.de</a> schrieb:</p>
<blockquote>
<p>Templates, wofür programmiert man sonst C++...</p>
</blockquote>
<p>eben <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f576.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--sunglasses"
      title=":sunglasses:"
      alt="🕶"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1195059</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1195059</guid><dc:creator><![CDATA[lolz]]></dc:creator><pubDate>Tue, 19 Dec 2006 19:30:02 GMT</pubDate></item><item><title><![CDATA[Reply to Variablen Datentyp zurückgeben on Wed, 20 Dec 2006 09:56:00 GMT]]></title><description><![CDATA[<p>Maurice Ch. schrieb:</p>
<blockquote>
<p>...</p>
<pre><code class="language-cpp">class Mixed
{
	private:
		int intVal;
		float floatVal;
		bool boolVal;
		char charVal;
		string strVal;
		
		char type;

	public:
		Mixed(int val) {
			this-&gt;intVal = val;
			this-&gt;type = 'i';
		}

		Mixed(float val) {
			this-&gt;floatVal = val;
			this-&gt;type = 'f';
		}

		Mixed(bool val) {
			this-&gt;boolVal = val;
			this-&gt;type = 'b';
		}

		Mixed(char val) {
			this-&gt;charVal = val;
			this-&gt;type = 'c';
		}

		Mixed(string val) {
			this-&gt;strVal = val;
			this-&gt;type = 's';
		}
};
</code></pre>
<p>...</p>
</blockquote>
<p>Oder</p>
<pre><code class="language-cpp">template &lt;typename T&gt;
class Mixed
{
   private:
      T value;
   public:
      typedef T value_type;
      Mixed(T val) : value(val) {}
      T&amp; get() { return value;  }      
};

// Das Schöne an Funktionstemplates ist, dass sie sich selbst den passenden Typ &quot;raussuchen&quot;
template &lt;typename T&gt;
Mixed&lt;T&gt; makeMixed(T t) { return t; }
</code></pre>
<p>Was Dir besser gefällt, kannst Du Dir selbst überlegen .. ich weiß, welches mir besser gefällt.<br />
<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>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1195251</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1195251</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Wed, 20 Dec 2006 09:56:00 GMT</pubDate></item><item><title><![CDATA[Reply to Variablen Datentyp zurückgeben on Wed, 20 Dec 2006 11:02:13 GMT]]></title><description><![CDATA[<p>bei der template variante seh ich ein problem mit bestimmten operationen. ganz simpel z.b. der + operator. Mixed + Mixed.</p>
<p>dabei gibts beispielsweise die konvention, dass beide Mixed zu float/int konvertiert und dann addiert werden.</p>
<p>aber was machst du jetzt, wenn einer der Mixed einen datentyp enthält, den du nicht bedacht hast? oder gott bewahre: ein eigener typ.</p>
<p>ohne templates kann man schon bei objekterzeugung dafür sorge tragen, dass nur erlaubte typen verwendet werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1195290</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1195290</guid><dc:creator><![CDATA[thordk]]></dc:creator><pubDate>Wed, 20 Dec 2006 11:02:13 GMT</pubDate></item><item><title><![CDATA[Reply to Variablen Datentyp zurückgeben on Wed, 20 Dec 2006 11:30:45 GMT]]></title><description><![CDATA[<p>Na, das template würde fordern, das es für die Klasse den operator+ geben muss.</p>
<p>Wenn die Klasse diesen nicht sinvoll zur Verfügung stellen kann, wird das template fehlschlagen.</p>
<p>Das wird aber auch eine Klasse wie am Anfang gezeigt, denn eine Klasse die Du nicht bedacht hast, wird auch bei dieser nicht funkionieren.</p>
<p>Ich sehe hier also nicht den Nachteil des Templates.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1195299</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1195299</guid><dc:creator><![CDATA[Knuddlbaer]]></dc:creator><pubDate>Wed, 20 Dec 2006 11:30:45 GMT</pubDate></item><item><title><![CDATA[Reply to Variablen Datentyp zurückgeben on Wed, 20 Dec 2006 12:08:31 GMT]]></title><description><![CDATA[<p>thordk schrieb:</p>
<blockquote>
<p>bei der template variante seh ich ein problem mit bestimmten operationen. ganz simpel z.b. der + operator. Mixed + Mixed....</p>
</blockquote>
<p>Da sehe ich gar keinen Unterschied zur &quot;von Hand-Methode&quot;. Man muss sich immer genau überlagen, was man mit den Typen machen können soll - und was nicht. Deswegen geht der Trend in C++ ja auch gerade hin zu &quot;stärkeren Typen&quot; ... und dem widerspricht halt das &quot;schwache Typenkonzept&quot; des OPs eigentlich.</p>
<p>Das template erspart letztlich nur &quot;Tipparbeit&quot; ... und sorgt manchmal dafür, dass man Dinge nicht vergisst.</p>
<p>Und wie Knuddl schon sagte: Wenn es keinen passenden operator gibt (geben soll), gibt's auch in der template-Variante einen Compilefehler ...</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1195328</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1195328</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Wed, 20 Dec 2006 12:08:31 GMT</pubDate></item><item><title><![CDATA[Reply to Variablen Datentyp zurückgeben on Wed, 20 Dec 2006 12:12:59 GMT]]></title><description><![CDATA[<p>Simon2 schrieb:</p>
<blockquote>
<p>Und wie Knuddl schon sagte: Wenn es keinen passenden operator gibt (geben soll), gibt's auch in der template-Variante einen Compilefehler ...</p>
</blockquote>
<p>Und das ist der Unterschied - wenn du zwei Mixed-Objekte zusammenfassen willst, die verschiedene Datentypen rumschleppen, merkst du das erst zur Laufzeit.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1195334</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1195334</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Wed, 20 Dec 2006 12:12:59 GMT</pubDate></item><item><title><![CDATA[Reply to Variablen Datentyp zurückgeben on Wed, 20 Dec 2006 12:31:38 GMT]]></title><description><![CDATA[<p>CStoll schrieb:</p>
<blockquote>
<p>Simon2 schrieb:</p>
<blockquote>
<p>Und wie Knuddl schon sagte: Wenn es keinen passenden operator gibt (geben soll), gibt's auch in der template-Variante einen Compilefehler ...</p>
</blockquote>
<p>Und das ist der Unterschied - wenn du zwei Mixed-Objekte zusammenfassen willst, die verschiedene Datentypen rumschleppen, merkst du das erst zur Laufzeit.</p>
</blockquote>
<p>Schön ausgedrückt.<br />
(Falls es nicht klar wurde: Ihc bin definitv für den template-Einsatz)</p>
<p>In Anlehnung an</p>
<p>thordk schrieb:</p>
<blockquote>
<p>...<br />
ohne templates kann man schon bei objekterzeugung dafür sorge tragen, dass nur erlaubte typen verwendet werden.</p>
</blockquote>
<p>würde ich sogar sagen:<br />
&quot;ohne templates <strong>muss</strong> man selbst dafür sorge tragen, dass nur erlaubte typen verwendet werden.&quot;<br />
(mit templates nimmt einem das der Compiler ab - und der ist IMMER besser in sowas)</p>
<p>Gruß,</p>
<p>Simon2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1195350</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1195350</guid><dc:creator><![CDATA[Simon2]]></dc:creator><pubDate>Wed, 20 Dec 2006 12:31:38 GMT</pubDate></item></channel></rss>