<?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[probleme mit enable_shared_from_this [gelöst]]]></title><description><![CDATA[<p>Hallo.</p>
<p>Ich möchte enable_shared_from_this verwenden, weil ich in einer abgeleitenen Basisklasse einen shared_ptr weitergeben möchte, aber leider bekomme ich das ganze nicht ordentlich hin <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>Hier ein Beispiel so wie bei mir der Anwendungsfall ist:</p>
<pre><code class="language-cpp">#include &lt;string&gt;
#include &quot;boost/enable_shared_from_this.hpp&quot;
#include &quot;boost/shared_ptr.hpp&quot;

class StoreData 
{
	public:
		std::string name_;
		std::string desc_;
	public:	
		StoreData(){}
};

template &lt;typename ClassData&gt;
class StoreClass
{
public:
	typedef boost::shared_ptr&lt;ClassData&gt; ClassDataPtr;
	std::vector&lt;ClassDataPtr&gt; storeData_;

public:
	StoreClass() {}
	void Add(const std::string &amp;a, const std::string &amp;b)
	{
		ClassDataPtr ptr(new ClassData());
		ptr-&gt;name_ = a;
		ptr-&gt;desc_ = b;
		storeData_.push_back(ptr);
	}
};

class MyStoreData;
typedef StoreClass&lt;MyStoreData&gt;::ClassDataPtr MyStoreDataPtr;
class MyStoreData : public boost::enable_shared_from_this&lt;MyStoreData&gt;, StoreData
{
	public:
		MyStoreData(){}
		MyStoreDataPtr GetPtr() { return shared_from_this();}
};

int main()
{
	StoreClass&lt;MyStoreData&gt; store;
	store.Add(&quot;abc&quot;, &quot;012&quot;);
}
</code></pre>
<p>Beim Compilieren bekomme ich folgende Fehler:</p>
<blockquote>
<p>test.h: In member function ‘void StoreClass&lt;ClassData&gt;::Add(const std::string&amp;, const std::string&amp;) [with ClassData = MyStoreData]’:<br />
test.cpp: instantiated from here<br />
test.h: error: ‘std::string StoreData::name_’ is inaccessible<br />
test.h: error: within this context<br />
test.h: error: ‘std::string StoreData::desc_’ is inaccessible<br />
test.h: error: within this context</p>
</blockquote>
<p>Mache ich in der Klasse MyStoreData Zugriffsmtehoden, dann sind die Fehlermeldugnen weg, aber warum?</p>
<p>interessant ist auch, wenn man MyStoreData folgend ableitet (Änderung der Reihenfolge der zu ableitenden Klassen)</p>
<pre><code class="language-cpp">class MyStoreData : public StoreData,  boost::enable_shared_from_this&lt;MyStoreData&gt;
</code></pre>
<p>dann sieht die Fehlermeldung des compilers so aus:</p>
<blockquote>
<p>In file included from /boost/boost/enable_shared_from_this.hpp:16,<br />
from test.h,<br />
from test.cpp:<br />
/boost/boost/smart_ptr/enable_shared_from_this.hpp: In member function ‘boost::shared_ptr&lt;X&gt; boost::enable_shared_from_this&lt;T&gt;::shared_from_this() [with T = MyStoreData]’:<br />
test.h: instantiated from here<br />
/boost/boost/smart_ptr/enable_shared_from_this.hpp:50: error: ‘boost::enable_shared_from_this&lt;MyStoreData&gt;’ is an inaccessible base of ‘MyStoreData’<br />
In file included from /boost/boost/smart_ptr/weak_ptr.hpp:18,<br />
from /boost/boost/smart_ptr/enable_shared_from_this.hpp:16,<br />
from /boost/boost/enable_shared_from_this.hpp:16,<br />
from test.h,<br />
from test.cpp:38:<br />
/boost/boost/smart_ptr/shared_ptr.hpp: In constructor ‘boost::shared_ptr&lt;T&gt;::shared_ptr(Y*) [with Y = MyStoreData, T = MyStoreData]’:<br />
test.h: instantiated from ‘void StoreClass&lt;ClassData&gt;::Add(const std::string&amp;, const std::string&amp;) [with ClassData = MyStoreData]’<br />
test.cpp: instantiated from here<br />
/boost/boost/smart_ptr/shared_ptr.hpp:189: error: ‘boost::enable_shared_from_this&lt;MyStoreData&gt;’ is an inaccessible base of ‘MyStoreData’</p>
</blockquote>
<p>Ist die Anwendung von enable_shared_from_this falsch oder beachte ich hier etwas nicht?<br />
ev. kann mir jemand weiter helfen? DANKE!</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/294072/probleme-mit-enable_shared_from_this-gelöst</link><generator>RSS for Node</generator><lastBuildDate>Sun, 16 Aug 2026 02:01:37 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/294072.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 17 Oct 2011 08:39:20 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to probleme mit enable_shared_from_this [gelöst] on Mon, 17 Oct 2011 08:52:46 GMT]]></title><description><![CDATA[<p>Hallo.</p>
<p>Ich möchte enable_shared_from_this verwenden, weil ich in einer abgeleitenen Basisklasse einen shared_ptr weitergeben möchte, aber leider bekomme ich das ganze nicht ordentlich hin <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>Hier ein Beispiel so wie bei mir der Anwendungsfall ist:</p>
<pre><code class="language-cpp">#include &lt;string&gt;
#include &quot;boost/enable_shared_from_this.hpp&quot;
#include &quot;boost/shared_ptr.hpp&quot;

class StoreData 
{
	public:
		std::string name_;
		std::string desc_;
	public:	
		StoreData(){}
};

template &lt;typename ClassData&gt;
class StoreClass
{
public:
	typedef boost::shared_ptr&lt;ClassData&gt; ClassDataPtr;
	std::vector&lt;ClassDataPtr&gt; storeData_;

public:
	StoreClass() {}
	void Add(const std::string &amp;a, const std::string &amp;b)
	{
		ClassDataPtr ptr(new ClassData());
		ptr-&gt;name_ = a;
		ptr-&gt;desc_ = b;
		storeData_.push_back(ptr);
	}
};

class MyStoreData;
typedef StoreClass&lt;MyStoreData&gt;::ClassDataPtr MyStoreDataPtr;
class MyStoreData : public boost::enable_shared_from_this&lt;MyStoreData&gt;, StoreData
{
	public:
		MyStoreData(){}
		MyStoreDataPtr GetPtr() { return shared_from_this();}
};

int main()
{
	StoreClass&lt;MyStoreData&gt; store;
	store.Add(&quot;abc&quot;, &quot;012&quot;);
}
</code></pre>
<p>Beim Compilieren bekomme ich folgende Fehler:</p>
<blockquote>
<p>test.h: In member function ‘void StoreClass&lt;ClassData&gt;::Add(const std::string&amp;, const std::string&amp;) [with ClassData = MyStoreData]’:<br />
test.cpp: instantiated from here<br />
test.h: error: ‘std::string StoreData::name_’ is inaccessible<br />
test.h: error: within this context<br />
test.h: error: ‘std::string StoreData::desc_’ is inaccessible<br />
test.h: error: within this context</p>
</blockquote>
<p>Mache ich in der Klasse MyStoreData Zugriffsmtehoden, dann sind die Fehlermeldugnen weg, aber warum?</p>
<p>interessant ist auch, wenn man MyStoreData folgend ableitet (Änderung der Reihenfolge der zu ableitenden Klassen)</p>
<pre><code class="language-cpp">class MyStoreData : public StoreData,  boost::enable_shared_from_this&lt;MyStoreData&gt;
</code></pre>
<p>dann sieht die Fehlermeldung des compilers so aus:</p>
<blockquote>
<p>In file included from /boost/boost/enable_shared_from_this.hpp:16,<br />
from test.h,<br />
from test.cpp:<br />
/boost/boost/smart_ptr/enable_shared_from_this.hpp: In member function ‘boost::shared_ptr&lt;X&gt; boost::enable_shared_from_this&lt;T&gt;::shared_from_this() [with T = MyStoreData]’:<br />
test.h: instantiated from here<br />
/boost/boost/smart_ptr/enable_shared_from_this.hpp:50: error: ‘boost::enable_shared_from_this&lt;MyStoreData&gt;’ is an inaccessible base of ‘MyStoreData’<br />
In file included from /boost/boost/smart_ptr/weak_ptr.hpp:18,<br />
from /boost/boost/smart_ptr/enable_shared_from_this.hpp:16,<br />
from /boost/boost/enable_shared_from_this.hpp:16,<br />
from test.h,<br />
from test.cpp:38:<br />
/boost/boost/smart_ptr/shared_ptr.hpp: In constructor ‘boost::shared_ptr&lt;T&gt;::shared_ptr(Y*) [with Y = MyStoreData, T = MyStoreData]’:<br />
test.h: instantiated from ‘void StoreClass&lt;ClassData&gt;::Add(const std::string&amp;, const std::string&amp;) [with ClassData = MyStoreData]’<br />
test.cpp: instantiated from here<br />
/boost/boost/smart_ptr/shared_ptr.hpp:189: error: ‘boost::enable_shared_from_this&lt;MyStoreData&gt;’ is an inaccessible base of ‘MyStoreData’</p>
</blockquote>
<p>Ist die Anwendung von enable_shared_from_this falsch oder beachte ich hier etwas nicht?<br />
ev. kann mir jemand weiter helfen? DANKE!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132389</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132389</guid><dc:creator><![CDATA[taff]]></dc:creator><pubDate>Mon, 17 Oct 2011 08:52:46 GMT</pubDate></item><item><title><![CDATA[Reply to probleme mit enable_shared_from_this [gelöst] on Mon, 17 Oct 2011 08:42:01 GMT]]></title><description><![CDATA[<p>Ich vermute mal, du musst bei beiden public hinschreiben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132390</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132390</guid><dc:creator><![CDATA[314159265358979]]></dc:creator><pubDate>Mon, 17 Oct 2011 08:42:01 GMT</pubDate></item><item><title><![CDATA[Reply to probleme mit enable_shared_from_this [gelöst] on Mon, 17 Oct 2011 08:52:19 GMT]]></title><description><![CDATA[<p>ach du schande, dass war ja nun ordentlich peinlich <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f621.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--pouting_face"
      title=":rage:"
      alt="😡"
    /> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /><br />
danke! da hab ich wieder mal den wald vor lauter bäumen nicht gsehen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2132399</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132399</guid><dc:creator><![CDATA[taff]]></dc:creator><pubDate>Mon, 17 Oct 2011 08:52:19 GMT</pubDate></item><item><title><![CDATA[Reply to probleme mit enable_shared_from_this [gelöst] on Mon, 17 Oct 2011 09:02:00 GMT]]></title><description><![CDATA[<p>Deine 'GetPtr()' Methode ist unnötig.<br />
Wenn du von boost::enable_shared_from_this&lt;&gt; ableitest, bekommt die abgeleitene Klasse eine public Methode shared_from_this().</p>
<p>Wenn du jedoch die 'GetPtr' Methode behalten willst, dann würde ich die 'shared_from_this()' Methode verstecken.</p>
<pre><code class="language-cpp">class X : public boost::enable_shared_from_this&lt;X&gt; {
private:
        using boost::enable_shared_from_this&lt;X&gt;::shared_from_this; // hiding method
public:
        boost::shared_ptr&lt;X&gt; GetPtr() {
                return shared_from_this();
        }
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2132404</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2132404</guid><dc:creator><![CDATA[Mentras]]></dc:creator><pubDate>Mon, 17 Oct 2011 09:02:00 GMT</pubDate></item></channel></rss>