<?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[Template verständnis]]></title><description><![CDATA[<p>Hallo !</p>
<p>Bin beim lesen eines Buches (C++ Codebook S.137) auf ein Problem gestoßen!<br />
<a href="http://www.pearson.ch/download/media/9783827322852_SP.pdf" rel="nofollow">http://www.pearson.ch/download/media/9783827322852_SP.pdf</a></p>
<p>Da steht:</p>
<pre><code>CMatrix(size_type rows, size_type columns, const Type &amp;obj=Type())
{
  resize(rows, columns, obj);
}
</code></pre>
<p>aber das Objekt m_elements ist doch noch nicht Inizialisiert. Wie kann man da schon auf resize zugreifen? Oder muss man das bei Templates nicht?</p>
<p>sollte doch so sein:</p>
<pre><code>CMatrix(size_type rows, size_type columns, const Type &amp;obj=Type())
: m_rows(rows), m_columns(columns){}
</code></pre>
<p>Bin ich Dumm oder der Autor? <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>
]]></description><link>https://www.c-plusplus.net/forum/topic/317612/template-verständnis</link><generator>RSS for Node</generator><lastBuildDate>Tue, 28 Jul 2026 22:03:58 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/317612.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 14 Jun 2013 12:57:27 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Template verständnis on Fri, 14 Jun 2013 12:57:27 GMT]]></title><description><![CDATA[<p>Hallo !</p>
<p>Bin beim lesen eines Buches (C++ Codebook S.137) auf ein Problem gestoßen!<br />
<a href="http://www.pearson.ch/download/media/9783827322852_SP.pdf" rel="nofollow">http://www.pearson.ch/download/media/9783827322852_SP.pdf</a></p>
<p>Da steht:</p>
<pre><code>CMatrix(size_type rows, size_type columns, const Type &amp;obj=Type())
{
  resize(rows, columns, obj);
}
</code></pre>
<p>aber das Objekt m_elements ist doch noch nicht Inizialisiert. Wie kann man da schon auf resize zugreifen? Oder muss man das bei Templates nicht?</p>
<p>sollte doch so sein:</p>
<pre><code>CMatrix(size_type rows, size_type columns, const Type &amp;obj=Type())
: m_rows(rows), m_columns(columns){}
</code></pre>
<p>Bin ich Dumm oder der Autor? <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>
]]></description><link>https://www.c-plusplus.net/forum/post/2331119</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331119</guid><dc:creator><![CDATA[xi.xi]]></dc:creator><pubDate>Fri, 14 Jun 2013 12:57:27 GMT</pubDate></item><item><title><![CDATA[Reply to Template verständnis on Fri, 14 Jun 2013 13:02:23 GMT]]></title><description><![CDATA[<blockquote>
<p>Bin ich Dumm oder der Autor?</p>
</blockquote>
<p>Du bist nicht dumm, nur unwissend.</p>
<blockquote>
<p>aber das Objekt m_elements ist doch noch nicht Inizialisiert. Wie kann man da schon auf resize zugreifen?</p>
</blockquote>
<p>m_elements wurde default-initialisiert.</p>
<p>In dem meisten Fällen gilt: Wenn eine (nicht-statische) Membervariable nicht im Konstruktor in der Initialisierungsliste initialisiert wird, wird sie default-initialisiert. Das hat auch nichts mit Templates zu tun.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2331125</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331125</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Fri, 14 Jun 2013 13:02:23 GMT</pubDate></item><item><title><![CDATA[Reply to Template verständnis on Fri, 14 Jun 2013 13:05:36 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p>In dem meisten Fällen gilt: Wenn eine (nicht-statische) Membervariable nicht im Konstruktor in der Initialisierungsliste initialisiert wird, wird sie default-initialisiert.</p>
</blockquote>
<p>In welchem Fall gilt es nicht?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2331129</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331129</guid><dc:creator><![CDATA[out]]></dc:creator><pubDate>Fri, 14 Jun 2013 13:05:36 GMT</pubDate></item><item><title><![CDATA[Reply to Template verständnis on Fri, 14 Jun 2013 13:30:36 GMT]]></title><description><![CDATA[<p>out schrieb:</p>
<blockquote>
<p>Sone schrieb:</p>
<blockquote>
<p>In dem meisten Fällen gilt: Wenn eine (nicht-statische) Membervariable nicht im Konstruktor in der Initialisierungsliste initialisiert wird, wird sie default-initialisiert.</p>
</blockquote>
<p>In welchem Fall gilt es nicht?</p>
</blockquote>
<blockquote>
<p>In a non-delegating constructor, if a given non-static data member [...] is not designated by a mem-initializer-id (including the case where there is no mem-initializer-list because the constructor has no ctor-initializer) and the entity is not a virtual base class of an abstract class (10.4), then</p>
<ul>
<li>if the entity is a non-static data member that has a brace-or-equal-initializer, the entity is initialized as specified in 8.5;</li>
<li>otherwise, if the entity is a variant member (9.5), no initialization is performed;</li>
<li>otherwise, the entity is default-initialized (8.5).</li>
</ul>
</blockquote>
<p>~Edit²: Nochmal Formatierung im Zitat korrigiert.~<br />
Beispiel:<br />
Seit C++11 ist folgendes möglich:</p>
<pre><code>struct Foo
{
    int bar = 5;
};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2331131</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331131</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Fri, 14 Jun 2013 13:30:36 GMT</pubDate></item><item><title><![CDATA[Reply to Template verständnis on Fri, 14 Jun 2013 13:33:51 GMT]]></title><description><![CDATA[<p>Hab das mal kurz getippt!</p>
<p>Mein Compiler will das nicht!</p>
<pre><code>#include &lt;cstdlib&gt;
#include &lt;iostream&gt;
#include &lt;vector&gt;

template&lt;typename Type, typename Allocator = std::allocator &lt; Type &gt; &gt;
class CSaveVector : public std::vector&lt;Type, Allocator&gt; {
public:
	typedef std::vector&lt;Type, Allocator&gt; base_type;
	typedef CSaveVector&lt;Type, Allocator&gt; my_type;
	typedef typename base_type::size_type size_type;
	typedef typename base_type::reference reference;
	typedef typename base_type::const_reference const_reference;

	//------------ constructoren ------------------

	explicit CSaveVector(const Allocator &amp;a = Allocator())
	: base_type(a){}

	CSaveVector(const my_type &amp;v)
	: base_type(v){}

	CSaveVector(size_type n, const Type &amp;obj=Type(), const Allocator &amp;a = Allocator())
	: base_type(n,obj,a){}

	template&lt;typename Input&gt;
	CSaveVector(Input begin, Input end, const Allocator &amp;a=Allocator())
	: base_type(begin, end, a){}

	//----------- operatoren ---------------

	reference operator[](size_type offset){
		return(at(offset));
	}

	const_reference operator[](size_type offset) const{
		return(at(offset));
	}
};

//=====================================================================================
template&lt;typename Type, typename Allocator &gt;
class CMatrix;
//=====================================================================================

template&lt;typename Type, typename Allocator = std::allocator&lt;Type&gt; &gt;
class CMatrixVector : private CSaveVector&lt;Type, Allocator&gt;{
	friend class CMatrix&lt;Type, Allocator&gt;;
public:
	typedef CSaveVector&lt;Type, Allocator&gt; base_type;
	typedef CMatrixVector&lt;Type, Allocator&gt; my_type;
	typedef typename base_type::size_type size_type;
	typedef typename base_type::reference reference;
	typedef typename base_type::const_reference const_reference;

	explicit CMatrixVector(const Allocator &amp;a=Allocator())
	: base_type(a){}

	CMatrixVector(size_type n, const Type &amp;obj=Type(), const Allocator &amp;a=Allocator())
	:base_type(n,obj,a){}

	reference operator[](size_type idx){
		return (base_type::operator[](idx));
	}
	const_reference operator[](size_type idx) const {
		return(base_type::operator[](idx));
	}
};

//=======================================================================

template&lt;typename Type, typename Allocator = std::allocator&lt;Type&gt; &gt;
class CMatrix : public std::vector&lt;std::vector&lt;Type&gt; &gt; {
public:
	typedef CMatrix&lt;Type, Allocator&gt; my_type;
	typedef std::size_t size_type;
	typedef Type&amp; reference;
	typedef const Type&amp; const_reference;

private:
	CMatrixVector&lt;CMatrixVector&lt;Type, Allocator&gt;, Allocator &gt; m_elements;
	size_type m_rows;
	size_type m_columns;

public:
	CMatrix(size_type rows, size_type columns, const Type &amp;obj=Type())
	{
		resize(rows, columns, obj);
	}

	void resize(size_type rows, size_type columns, const Type &amp;obj=Type()){

			/* Größe des Zeilenvektors anpassen */
			m_elements.resize(rows, CMatrixVector&lt;Type, Allocator&gt;(columns,obj));

			/* Größe der Spaltenvektoren anpassen */
			for(size_type r=0; r&lt;rows; ++r)
				m_elements[r].resize(columns,obj);

			m_columns = columns;
			m_rows = rows;
	}
};

int main (int argc, char* argv[]){

	CMatrix&lt;int&gt; m(2,2);

	return EXIT_FAILURE;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2331142</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331142</guid><dc:creator><![CDATA[xi.xi]]></dc:creator><pubDate>Fri, 14 Jun 2013 13:33:51 GMT</pubDate></item><item><title><![CDATA[Reply to Template verständnis on Fri, 14 Jun 2013 13:43:41 GMT]]></title><description><![CDATA[<p>Steht das so im Buch <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f62e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_open_mouth"
      title=":open_mouth:"
      alt="😮"
    /> Sofort wegwerfen.</p>
<p>Dieses</p>
<pre><code class="language-cpp">public std::vector&lt;Type, Allocator&gt;
</code></pre>
<p>ist die Ursache aller Probleme.</p>
<p>Das unmittelbare Problem ist die nicht-öffentliche Vererbung in</p>
<pre><code class="language-cpp">class CMatrixVector : private CSaveVector&lt;Type, Allocator&gt;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2331147</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331147</guid><dc:creator><![CDATA[ellol]]></dc:creator><pubDate>Fri, 14 Jun 2013 13:43:41 GMT</pubDate></item><item><title><![CDATA[Reply to Template verständnis on Fri, 14 Jun 2013 14:34:38 GMT]]></title><description><![CDATA[<p>Danke ellol !</p>
<p>Ja, stand so drin!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2331176</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331176</guid><dc:creator><![CDATA[xi.xi]]></dc:creator><pubDate>Fri, 14 Jun 2013 14:34:38 GMT</pubDate></item><item><title><![CDATA[Reply to Template verständnis on Fri, 14 Jun 2013 14:58:11 GMT]]></title><description><![CDATA[<p>Ich habs mal genauer angeschaut.</p>
<p>Fehleranalyse:</p>
<pre><code class="language-cpp">template&lt;typename Type, typename Allocator = std::allocator&lt;Type&gt; &gt; 
class CMatrixVector : private CSaveVector&lt;Type, Allocator&gt;{ 
  friend class CMatrix;
</code></pre>
<p>Geht nicht, weil <code>CMatrix</code> ein Template ist. So stands im Buch.</p>
<pre><code class="language-cpp">friend class CMatrix&lt;Type, Allocator&gt;;
</code></pre>
<p>Ist zwar erlaubt, aber das bedeutet, dass für</p>
<pre><code class="language-cpp">CMatrixVector&lt;CMatrixVector&lt;Type, Allocator&gt;, Allocator &gt; m_elements;
</code></pre>
<p>der Freund <code>CMatrix&lt;CMatrixVector&lt;Type, Allocator&gt;, Allocator &gt;</code> heisst. Das passt aber nicht auf <code>CMatrix&lt;Type, Allocator&gt;</code> , was gebraucht wird.</p>
<p>Lösung:</p>
<pre><code class="language-cpp">template&lt;typename Type2, typename Allocator2&gt; 
  friend class CMatrix;
</code></pre>
<p>Gibt aber noch einen weiteren Fehler:</p>
<pre><code class="language-cpp">reference operator[](size_type offset){ 
        return(at(offset)); 
    }
</code></pre>
<p>muss</p>
<pre><code class="language-cpp">reference operator[](size_type offset){ 
        return(this-&gt;at(offset)); 
    }
</code></pre>
<p>heissen.</p>
<p>Jetzt gehts.</p>
<p>...</p>
<p>Zum Inhalt.</p>
<p>Dein Buch missbraucht Vererbung. Hier sind einige Argumente, weshalb Vererbung in diesem Zusammenhang GAR KEINEN Sinn ergibt: <a href="http://stackoverflow.com/questions/6806173/subclass-inherit-standard-containers" rel="nofollow">http://stackoverflow.com/questions/6806173/subclass-inherit-standard-containers</a></p>
<p>Das Buch lehrt auch veralteten Stil. std::unary_function ist offiziell deprecated. Klassen mit &quot;C&quot; zu präfixen ist veralteter Stil der heute kaum noch anzutreffen ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2331182</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2331182</guid><dc:creator><![CDATA[ellol]]></dc:creator><pubDate>Fri, 14 Jun 2013 14:58:11 GMT</pubDate></item></channel></rss>