<?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[Container für mehrere Typen auf einmal]]></title><description><![CDATA[<p>Hallo <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="🙂"
    /><br />
Ich habe im Versuch einen Container zu schreiben, der Objekte von verschiedenen Typen gleichzeitig beinhalten kann, folgende Klassen geschrieben. Hier erstmal der Code :</p>
<pre><code>#ifndef I_PROPERTY_HPP
#define I_PROPERTY_HPP

#include &lt;string&gt;
#include &lt;typeindex&gt;
#include &lt;type_traits&gt;

class IProperty
{
	public:
		IProperty(const std::string id, const std::type_index t_index);
		virtual ~IProperty();

		const std::string     getId()        const;
		const std::type_index getTypeIndex() const;

	protected:
	private:
		const std::string     id_;
		const std::type_index t_index_;

}; // class IProperty

#endif // I_PROPERTY_HPP
</code></pre>
<pre><code>#include &quot;IProperty.hpp&quot;

IProperty::IProperty( const std::string id, const std::type_index t_index)
	: id_(id), t_index_(t_index)
{}

IProperty::~IProperty()
{}

const std::string IProperty::getId() const
{
	return id_;
}

const std::type_index IProperty::getTypeIndex() const
{
	return t_index_;
}
</code></pre>
<pre><code>#ifndef T_PROPERTY_HPP
#define T_PROPERTY_HPP

#include &lt;typeindex&gt;

#include &quot;IProperty.hpp&quot;

template &lt; typename Type &gt; 
class TProperty : public IProperty
{
	public:
		TProperty(const std::string id) : IProperty(id, std::type_index(typeid(Type))){}

		Type getValue()           const { return value_; }
		void setValue(Type value)       { value_ = value; }

	protected:
	private:
		Type value_;

}; // class TProperty

#endif // T_PROPERTY_HPP
</code></pre>
<pre><code>#ifndef PROPERTY_MANAGER_HPP
#define PROPERTY_MANAGER_HPP

#include &lt;iostream&gt;
#include &lt;functional&gt;
#include &lt;map&gt;
#include &lt;memory&gt;
#include &lt;stdexcept&gt;
#include &lt;string&gt;

#include &quot;IProperty.hpp&quot;
#include &quot;TProperty.hpp&quot;

class PropertyManager
{
	public: 
		PropertyManager();

		template &lt; typename Type &gt; 
		Type getPropertyValue(const std::string id) const {
			if (properties_.find(id) != properties_.end()){
				return std::dynamic_pointer_cast&lt;TProperty&lt;Type&gt;&gt;(properties_.at(id))-&gt;getValue();
			}
			throw std::invalid_argument(&quot;Property \&quot;&quot; + id + &quot;\&quot; not found!&quot;);
		}

		template &lt; typename Type &gt; 
		void setPropertyValue(const std::string id, Type value){
			if (properties_.find(id) != properties_.end()){
				std::dynamic_pointer_cast&lt;TProperty&lt;Type&gt;&gt;(properties_.at(id))-&gt;setValue(value);
			} else
			throw std::invalid_argument(&quot;Property \&quot;&quot; + id + &quot;\&quot; not found!&quot;);
		}

		template &lt; typename Type &gt;
		void addProperty(const std::string id, Type value){
			if (properties_.find(id) == properties_.end()){
				properties_.emplace(id, std::make_shared&lt;TProperty&lt;Type&gt;&gt;(id));
				std::dynamic_pointer_cast&lt;TProperty&lt;Type&gt;&gt;(properties_.at(id))-&gt;setValue(value);
			} else
			throw std::invalid_argument(&quot;Property \&quot;&quot; + id + &quot;\&quot; already exists!&quot;);
		}

		std::function&lt;void(const std::string, bool)&gt;   addBool;
		std::function&lt;void(const std::string, char)&gt;   addChar;
		std::function&lt;void(const std::string, int)&gt;    addInt;
		std::function&lt;void(const std::string, float)&gt;  addFloat;
		std::function&lt;void(const std::string, double)&gt; addDouble;

	protected:
	private:
		std::map&lt;const std::string, std::shared_ptr&lt;IProperty&gt;&gt; properties_;

}; // class PropertyManager

#endif //PROPERTY_MANAGER_HPP
</code></pre>
<pre><code>#include &quot;PropertyManager.hpp&quot;

PropertyManager::PropertyManager()
	: addBool   { std::bind(&amp;PropertyManager::addProperty&lt;bool&gt;,   this, std::placeholders::_1, std::placeholders::_2) },
	  addChar   { std::bind(&amp;PropertyManager::addProperty&lt;char&gt;,   this, std::placeholders::_1, std::placeholders::_2) },
	  addInt    { std::bind(&amp;PropertyManager::addProperty&lt;int&gt;,    this, std::placeholders::_1, std::placeholders::_2) },
	  addFloat  { std::bind(&amp;PropertyManager::addProperty&lt;float&gt;,  this, std::placeholders::_1, std::placeholders::_2) },
	  addDouble { std::bind(&amp;PropertyManager::addProperty&lt;double&gt;, this, std::placeholders::_1, std::placeholders::_2) }
{}
</code></pre>
<p>Jetzt habe ich zwei Fragen.<br />
Erstens, ist die Umsetzung des Containers grundsätzlich akzeptabel oder gibt es eine effektivere Methode dies zu erreichen ?<br />
Zweitens, im letzten Codeblock initialisiere ich die &quot;Synonyme&quot; für die Funktion<br />
&quot;addProperty&quot;. Gibt es hier auch eine andere Möglichkeit dies umzusetzen ? ( typedef scheint keine Option zu sein )</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/336983/container-für-mehrere-typen-auf-einmal</link><generator>RSS for Node</generator><lastBuildDate>Sat, 18 Apr 2026 15:26:03 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/336983.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 28 Feb 2016 16:25:48 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Container für mehrere Typen auf einmal on Sun, 28 Feb 2016 16:25:48 GMT]]></title><description><![CDATA[<p>Hallo <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="🙂"
    /><br />
Ich habe im Versuch einen Container zu schreiben, der Objekte von verschiedenen Typen gleichzeitig beinhalten kann, folgende Klassen geschrieben. Hier erstmal der Code :</p>
<pre><code>#ifndef I_PROPERTY_HPP
#define I_PROPERTY_HPP

#include &lt;string&gt;
#include &lt;typeindex&gt;
#include &lt;type_traits&gt;

class IProperty
{
	public:
		IProperty(const std::string id, const std::type_index t_index);
		virtual ~IProperty();

		const std::string     getId()        const;
		const std::type_index getTypeIndex() const;

	protected:
	private:
		const std::string     id_;
		const std::type_index t_index_;

}; // class IProperty

#endif // I_PROPERTY_HPP
</code></pre>
<pre><code>#include &quot;IProperty.hpp&quot;

IProperty::IProperty( const std::string id, const std::type_index t_index)
	: id_(id), t_index_(t_index)
{}

IProperty::~IProperty()
{}

const std::string IProperty::getId() const
{
	return id_;
}

const std::type_index IProperty::getTypeIndex() const
{
	return t_index_;
}
</code></pre>
<pre><code>#ifndef T_PROPERTY_HPP
#define T_PROPERTY_HPP

#include &lt;typeindex&gt;

#include &quot;IProperty.hpp&quot;

template &lt; typename Type &gt; 
class TProperty : public IProperty
{
	public:
		TProperty(const std::string id) : IProperty(id, std::type_index(typeid(Type))){}

		Type getValue()           const { return value_; }
		void setValue(Type value)       { value_ = value; }

	protected:
	private:
		Type value_;

}; // class TProperty

#endif // T_PROPERTY_HPP
</code></pre>
<pre><code>#ifndef PROPERTY_MANAGER_HPP
#define PROPERTY_MANAGER_HPP

#include &lt;iostream&gt;
#include &lt;functional&gt;
#include &lt;map&gt;
#include &lt;memory&gt;
#include &lt;stdexcept&gt;
#include &lt;string&gt;

#include &quot;IProperty.hpp&quot;
#include &quot;TProperty.hpp&quot;

class PropertyManager
{
	public: 
		PropertyManager();

		template &lt; typename Type &gt; 
		Type getPropertyValue(const std::string id) const {
			if (properties_.find(id) != properties_.end()){
				return std::dynamic_pointer_cast&lt;TProperty&lt;Type&gt;&gt;(properties_.at(id))-&gt;getValue();
			}
			throw std::invalid_argument(&quot;Property \&quot;&quot; + id + &quot;\&quot; not found!&quot;);
		}

		template &lt; typename Type &gt; 
		void setPropertyValue(const std::string id, Type value){
			if (properties_.find(id) != properties_.end()){
				std::dynamic_pointer_cast&lt;TProperty&lt;Type&gt;&gt;(properties_.at(id))-&gt;setValue(value);
			} else
			throw std::invalid_argument(&quot;Property \&quot;&quot; + id + &quot;\&quot; not found!&quot;);
		}

		template &lt; typename Type &gt;
		void addProperty(const std::string id, Type value){
			if (properties_.find(id) == properties_.end()){
				properties_.emplace(id, std::make_shared&lt;TProperty&lt;Type&gt;&gt;(id));
				std::dynamic_pointer_cast&lt;TProperty&lt;Type&gt;&gt;(properties_.at(id))-&gt;setValue(value);
			} else
			throw std::invalid_argument(&quot;Property \&quot;&quot; + id + &quot;\&quot; already exists!&quot;);
		}

		std::function&lt;void(const std::string, bool)&gt;   addBool;
		std::function&lt;void(const std::string, char)&gt;   addChar;
		std::function&lt;void(const std::string, int)&gt;    addInt;
		std::function&lt;void(const std::string, float)&gt;  addFloat;
		std::function&lt;void(const std::string, double)&gt; addDouble;

	protected:
	private:
		std::map&lt;const std::string, std::shared_ptr&lt;IProperty&gt;&gt; properties_;

}; // class PropertyManager

#endif //PROPERTY_MANAGER_HPP
</code></pre>
<pre><code>#include &quot;PropertyManager.hpp&quot;

PropertyManager::PropertyManager()
	: addBool   { std::bind(&amp;PropertyManager::addProperty&lt;bool&gt;,   this, std::placeholders::_1, std::placeholders::_2) },
	  addChar   { std::bind(&amp;PropertyManager::addProperty&lt;char&gt;,   this, std::placeholders::_1, std::placeholders::_2) },
	  addInt    { std::bind(&amp;PropertyManager::addProperty&lt;int&gt;,    this, std::placeholders::_1, std::placeholders::_2) },
	  addFloat  { std::bind(&amp;PropertyManager::addProperty&lt;float&gt;,  this, std::placeholders::_1, std::placeholders::_2) },
	  addDouble { std::bind(&amp;PropertyManager::addProperty&lt;double&gt;, this, std::placeholders::_1, std::placeholders::_2) }
{}
</code></pre>
<p>Jetzt habe ich zwei Fragen.<br />
Erstens, ist die Umsetzung des Containers grundsätzlich akzeptabel oder gibt es eine effektivere Methode dies zu erreichen ?<br />
Zweitens, im letzten Codeblock initialisiere ich die &quot;Synonyme&quot; für die Funktion<br />
&quot;addProperty&quot;. Gibt es hier auch eine andere Möglichkeit dies umzusetzen ? ( typedef scheint keine Option zu sein )</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2488904</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2488904</guid><dc:creator><![CDATA[xxXLukas97Xxx]]></dc:creator><pubDate>Sun, 28 Feb 2016 16:25:48 GMT</pubDate></item><item><title><![CDATA[Reply to Container für mehrere Typen auf einmal on Sun, 28 Feb 2016 17:44:21 GMT]]></title><description><![CDATA[<p>Nach IProperty hab ich aufgehört:<br />
- I, dann aber keine abstrakten Funktionen - merkwürdig<br />
- call by value im Konstruktor, aber const?<br />
- const Returnwerte, die weder Referenz noch Pointer sind?<br />
- const Member?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2488912</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2488912</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Sun, 28 Feb 2016 17:44:21 GMT</pubDate></item><item><title><![CDATA[Reply to Container für mehrere Typen auf einmal on Sun, 28 Feb 2016 18:07:50 GMT]]></title><description><![CDATA[<p>Was ist der Zweck von dem ganzen? Wenn du verschiedene Typen speichern willst, könntest du boost::variant oder boost::any verwenden (bzw., es gibt bessere Klassen als boost::any, das hat keine Optimierung für kleine Typen). Was du gemacht hast, schaut schon so ein bisschen wie boost::any aus, aber ziemlich schlecht umgesetzt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2488915</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2488915</guid><dc:creator><![CDATA[Mechanics]]></dc:creator><pubDate>Sun, 28 Feb 2016 18:07:50 GMT</pubDate></item></channel></rss>