<?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[Frage zur Rückgabe von const-Liste mit Smart-Pointern]]></title><description><![CDATA[<p>Hiho,</p>
<p>folgende Ausgangslage: ich habe eine Klasse, welche mehrere Heapobjekte in einer Liste verwaltet. Die Liste benutzt dazu Smartpointer aus boost. Nun möchte ich die Liste nach außen geben, damit andere LESEND darauf zugreifen können.<br />
Folgender Beispielcode, der das veranschauchlicht:</p>
<pre><code class="language-cpp">class WithSharedPtr
{
	public:
		//
		typedef std::list&lt;boost::shared_ptr&lt;int&gt;&gt; IntList;

		//
		WithSharedPtr()
		{
			m_spList.push_back(boost::make_shared&lt;int&gt;(3));
			m_spList.push_back(boost::make_shared&lt;int&gt;(5));
		}

		//
		const IntList&amp; getListPtr() const { return m_spList; }

	private:
		//
		IntList m_spList;
};

int main()
{
	WithSharedPtr ptr;
	*ptr.getListPtr().front() = 2;
...
}
</code></pre>
<p>Wie man nun sieht, kann ich die Objekte der Smartpointer ändern. Warum, das ist mir klar. Meine Frage ist: wie kann man das am besten lösen, das ich die Liste herausgebe und verhinder, dass der Aufrufer die Objekte verändern kann? Bisher ist mir nur eingefallen eine zweite Liste auf const int* (wenn ich beim Bsp. bleibe) zu erzeugen, die Smartpoinetr zu kopieren und diese Liste herauszugeben. Aber ich will an sich nicht eine zweite Liste erzeugen.</p>
<p>VG</p>
<p>Pellaeon</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/293282/frage-zur-rückgabe-von-const-liste-mit-smart-pointern</link><generator>RSS for Node</generator><lastBuildDate>Sun, 16 Aug 2026 10:52:56 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/293282.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 29 Sep 2011 13:20:26 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Frage zur Rückgabe von const-Liste mit Smart-Pointern on Thu, 29 Sep 2011 13:20:26 GMT]]></title><description><![CDATA[<p>Hiho,</p>
<p>folgende Ausgangslage: ich habe eine Klasse, welche mehrere Heapobjekte in einer Liste verwaltet. Die Liste benutzt dazu Smartpointer aus boost. Nun möchte ich die Liste nach außen geben, damit andere LESEND darauf zugreifen können.<br />
Folgender Beispielcode, der das veranschauchlicht:</p>
<pre><code class="language-cpp">class WithSharedPtr
{
	public:
		//
		typedef std::list&lt;boost::shared_ptr&lt;int&gt;&gt; IntList;

		//
		WithSharedPtr()
		{
			m_spList.push_back(boost::make_shared&lt;int&gt;(3));
			m_spList.push_back(boost::make_shared&lt;int&gt;(5));
		}

		//
		const IntList&amp; getListPtr() const { return m_spList; }

	private:
		//
		IntList m_spList;
};

int main()
{
	WithSharedPtr ptr;
	*ptr.getListPtr().front() = 2;
...
}
</code></pre>
<p>Wie man nun sieht, kann ich die Objekte der Smartpointer ändern. Warum, das ist mir klar. Meine Frage ist: wie kann man das am besten lösen, das ich die Liste herausgebe und verhinder, dass der Aufrufer die Objekte verändern kann? Bisher ist mir nur eingefallen eine zweite Liste auf const int* (wenn ich beim Bsp. bleibe) zu erzeugen, die Smartpoinetr zu kopieren und diese Liste herauszugeben. Aber ich will an sich nicht eine zweite Liste erzeugen.</p>
<p>VG</p>
<p>Pellaeon</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2125093</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2125093</guid><dc:creator><![CDATA[Pellaeon]]></dc:creator><pubDate>Thu, 29 Sep 2011 13:20:26 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zur Rückgabe von const-Liste mit Smart-Pointern on Thu, 29 Sep 2011 13:34:58 GMT]]></title><description><![CDATA[<p>proxy</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2125097</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2125097</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Thu, 29 Sep 2011 13:34:58 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zur Rückgabe von const-Liste mit Smart-Pointern on Thu, 29 Sep 2011 15:49:49 GMT]]></title><description><![CDATA[<p>siehe <a href="http://www.boost.org/doc/libs/1_47_0/libs/iterator/doc/indirect_iterator.html" rel="nofollow">http://www.boost.org/doc/libs/1_47_0/libs/iterator/doc/indirect_iterator.html</a></p>
<pre><code class="language-cpp">class WithSharedPtr {
  ...

  typedef boost::indirect_iterator&lt;
            std::list&lt;boost::shared_ptr&lt;int&gt; &gt;::const_iterator,
            const int // &lt;-- man beachte das 'const' hier
          &gt; const_iterator;

  const_iterator begin() const {return const_iterator(this-&gt;IntList.begin());}
  const_iterator end()   const {return const_iterator(this-&gt;IntList.end());  }

  ...
};
</code></pre>
<p>(ungetestet)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2125156</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2125156</guid><dc:creator><![CDATA[krümelkacker]]></dc:creator><pubDate>Thu, 29 Sep 2011 15:49:49 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zur Rückgabe von const-Liste mit Smart-Pointern on Thu, 29 Sep 2011 15:57:40 GMT]]></title><description><![CDATA[<p>- mist -</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2125161</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2125161</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Thu, 29 Sep 2011 15:57:40 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zur Rückgabe von const-Liste mit Smart-Pointern on Fri, 30 Sep 2011 06:12:29 GMT]]></title><description><![CDATA[<p>Danke für die Tipps. Das mit dem indirect_iterator klappt wunderbar und ist schneller getippt als einen eigenen Proxy zu schreiben <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/2125344</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2125344</guid><dc:creator><![CDATA[Pellaeon]]></dc:creator><pubDate>Fri, 30 Sep 2011 06:12:29 GMT</pubDate></item></channel></rss>