<?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 zu Effektiv c++ Programmieren, Konstruktor Template]]></title><description><![CDATA[<p>Hi, ich versuche mich gerade an einem Kapitel im oben genannten Buch, wo einem intelligenten Zeiger über ein Copy Constructor die Umwandlung in eine andere Instantiierung gleichen Templates erlaubt wird, allerdings nur so, dass das Konvertierungsverhalten eines normalen Zeigers nachgeahmt wird. Aber irgendwie bekomm ich meinen Code nicht zum laufen. Habe mir Teile von shared_ptr angeguckt, jedoch ohne Erfolg</p>
<p>meine Konstruktoren:</p>
<pre><code class="language-cpp">template&lt;typename U&gt;
		explicit IHandle ( U* pt ) try : ptr( pt ), ref_counter( new size_type(1)) 
		{

		} catch( std::bad_alloc&amp;)
		  { }

		template&lt;typename U&gt;
		IHandle( const IHandle&lt;U&gt;&amp; orig ) : ptr( orig.get() ), ref_counter( orig.refcounter ) 
		{
			++*ref_counter;
		}
		IHandle( const IHandle&amp; orig ) : ptr( orig.get() ), ref_counter( orig.ref_counter ) 
		{
			++*ref_counter;
		}
// in main
IHandle&lt;Base&gt; pt = IHandle&lt;Derived&gt;(new Derived);
//wobei Base und Derived inhaltlose Klassen sind, öffentlich vererbt
</code></pre>
<p>Fehlermeldung:<br />
: error C2662: 'nsHandle::IHandle&lt;T&gt;::get': this-Zeiger kann nicht von 'const nsHandle::IHandle&lt;T&gt;' in 'nsHandle::IHandle&lt;T&gt; &amp;' konvertiert werden<br />
1&gt; with<br />
1&gt; [<br />
1&gt; T=Derived<br />
1&gt; ]<br />
1&gt; Durch die Konvertierung gehen Qualifizierer verloren<br />
1&gt; Siehe Verweis auf die Instanziierung der gerade kompilierten Funktions-template &quot;nsHandle::IHandle&lt;T&gt;::IHandle&lt;Derived&gt;(const nsHandle::IHandle&lt;Derived&gt; &amp;)&quot;.<br />
1&gt; with<br />
1&gt; [<br />
1&gt; T=Base<br />
1&gt; ]</p>
<p>kann mir jemand helfen?<br />
Gruss<br />
Chris</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/229365/frage-zu-effektiv-c-programmieren-konstruktor-template</link><generator>RSS for Node</generator><lastBuildDate>Mon, 28 Sep 2026 00:45:04 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/229365.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 10 Dec 2008 19:33:59 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Frage zu Effektiv c++ Programmieren, Konstruktor Template on Wed, 10 Dec 2008 19:33:59 GMT]]></title><description><![CDATA[<p>Hi, ich versuche mich gerade an einem Kapitel im oben genannten Buch, wo einem intelligenten Zeiger über ein Copy Constructor die Umwandlung in eine andere Instantiierung gleichen Templates erlaubt wird, allerdings nur so, dass das Konvertierungsverhalten eines normalen Zeigers nachgeahmt wird. Aber irgendwie bekomm ich meinen Code nicht zum laufen. Habe mir Teile von shared_ptr angeguckt, jedoch ohne Erfolg</p>
<p>meine Konstruktoren:</p>
<pre><code class="language-cpp">template&lt;typename U&gt;
		explicit IHandle ( U* pt ) try : ptr( pt ), ref_counter( new size_type(1)) 
		{

		} catch( std::bad_alloc&amp;)
		  { }

		template&lt;typename U&gt;
		IHandle( const IHandle&lt;U&gt;&amp; orig ) : ptr( orig.get() ), ref_counter( orig.refcounter ) 
		{
			++*ref_counter;
		}
		IHandle( const IHandle&amp; orig ) : ptr( orig.get() ), ref_counter( orig.ref_counter ) 
		{
			++*ref_counter;
		}
// in main
IHandle&lt;Base&gt; pt = IHandle&lt;Derived&gt;(new Derived);
//wobei Base und Derived inhaltlose Klassen sind, öffentlich vererbt
</code></pre>
<p>Fehlermeldung:<br />
: error C2662: 'nsHandle::IHandle&lt;T&gt;::get': this-Zeiger kann nicht von 'const nsHandle::IHandle&lt;T&gt;' in 'nsHandle::IHandle&lt;T&gt; &amp;' konvertiert werden<br />
1&gt; with<br />
1&gt; [<br />
1&gt; T=Derived<br />
1&gt; ]<br />
1&gt; Durch die Konvertierung gehen Qualifizierer verloren<br />
1&gt; Siehe Verweis auf die Instanziierung der gerade kompilierten Funktions-template &quot;nsHandle::IHandle&lt;T&gt;::IHandle&lt;Derived&gt;(const nsHandle::IHandle&lt;Derived&gt; &amp;)&quot;.<br />
1&gt; with<br />
1&gt; [<br />
1&gt; T=Base<br />
1&gt; ]</p>
<p>kann mir jemand helfen?<br />
Gruss<br />
Chris</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1628443</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1628443</guid><dc:creator><![CDATA[Chris19]]></dc:creator><pubDate>Wed, 10 Dec 2008 19:33:59 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu Effektiv c++ Programmieren, Konstruktor Template on Wed, 10 Dec 2008 20:05:00 GMT]]></title><description><![CDATA[<p>Die Funktion <code>IHandle&lt;T&gt;::get</code> ist nicht konstant. Mach die konstant, dann geht dein Code und ich nehme schwer an, dass die Funktion auch konstant gemacht werden kann <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p>Grüssli</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1628453</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1628453</guid><dc:creator><![CDATA[Dravere]]></dc:creator><pubDate>Wed, 10 Dec 2008 20:05:00 GMT</pubDate></item></channel></rss>