<?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[move assignment operator]]></title><description><![CDATA[<p>hi,<br />
bitte um feedback um move assignment operator richtig zu implementieren:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;algorithm&gt;
#include &lt;cassert&gt;
using namespace std;

#define SIZE 256

class Stack {
private:
	unsigned int count;

public:
	vector&lt;int&gt; val;

	Stack(): val(SIZE), count(0) {}

	Stack(const Stack &amp;s): val(SIZE) {
		std::copy(s.val.begin(), s.val.end(), val.begin());
		count = s.count;
	}

	Stack&amp; operator=(const Stack &amp;s) {
		if(this != &amp;s) {
			std::copy(s.val.begin(), s.val.end(), val.begin());
			count = s.count;
		}

		return *this;
	}

	Stack&amp; operator=(Stack &amp;&amp;s) {
		if(this != &amp;s) {
			val = s.val;
			count = s.count;

			assert(val.size() == s.val.size());
		}

		return *this;
	}

	void push(int value) {
		if(count &lt; SIZE) {		
			val[count] = value;
			count++;
			return; 
		}

		throw(&quot;stack is full&quot;);
	}	

	int pop() {
		if(count &gt; 0) {
			int element = val[count-1];
			count--;
			return element;
		}

		throw(&quot;stack is empty&quot;);
	}

	bool empty() {
		return count == 0;
	}
};

int main() {
	Stack s;

	s.push(1);
	s.push(2);
	s.push(3);

	Stack a;
	a = s;

	while(!a.empty()) {
		cout &lt;&lt; a.pop() &lt;&lt; endl;
	}

	Stack b;
	b = std::move(s);

	while(!b.empty()) {
		cout &lt;&lt; b.pop() &lt;&lt; endl;
	}

	Stack c(s);
	while(!c.empty()) {
		cout &lt;&lt; c.pop() &lt;&lt; endl;
	}

	return 0;	
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/325551/move-assignment-operator</link><generator>RSS for Node</generator><lastBuildDate>Sat, 11 Jul 2026 20:49:45 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/325551.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 07 May 2014 13:10:38 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to move assignment operator on Wed, 07 May 2014 13:10:38 GMT]]></title><description><![CDATA[<p>hi,<br />
bitte um feedback um move assignment operator richtig zu implementieren:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;algorithm&gt;
#include &lt;cassert&gt;
using namespace std;

#define SIZE 256

class Stack {
private:
	unsigned int count;

public:
	vector&lt;int&gt; val;

	Stack(): val(SIZE), count(0) {}

	Stack(const Stack &amp;s): val(SIZE) {
		std::copy(s.val.begin(), s.val.end(), val.begin());
		count = s.count;
	}

	Stack&amp; operator=(const Stack &amp;s) {
		if(this != &amp;s) {
			std::copy(s.val.begin(), s.val.end(), val.begin());
			count = s.count;
		}

		return *this;
	}

	Stack&amp; operator=(Stack &amp;&amp;s) {
		if(this != &amp;s) {
			val = s.val;
			count = s.count;

			assert(val.size() == s.val.size());
		}

		return *this;
	}

	void push(int value) {
		if(count &lt; SIZE) {		
			val[count] = value;
			count++;
			return; 
		}

		throw(&quot;stack is full&quot;);
	}	

	int pop() {
		if(count &gt; 0) {
			int element = val[count-1];
			count--;
			return element;
		}

		throw(&quot;stack is empty&quot;);
	}

	bool empty() {
		return count == 0;
	}
};

int main() {
	Stack s;

	s.push(1);
	s.push(2);
	s.push(3);

	Stack a;
	a = s;

	while(!a.empty()) {
		cout &lt;&lt; a.pop() &lt;&lt; endl;
	}

	Stack b;
	b = std::move(s);

	while(!b.empty()) {
		cout &lt;&lt; b.pop() &lt;&lt; endl;
	}

	Stack c(s);
	while(!c.empty()) {
		cout &lt;&lt; c.pop() &lt;&lt; endl;
	}

	return 0;	
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2397895</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397895</guid><dc:creator><![CDATA[niko1]]></dc:creator><pubDate>Wed, 07 May 2014 13:10:38 GMT</pubDate></item><item><title><![CDATA[Reply to move assignment operator on Wed, 07 May 2014 13:40:31 GMT]]></title><description><![CDATA[<p>Dein move ist ein copy, inklusive assert: bei einem move wäre die Bedingung <strong>nicht</strong> erfüllt.<br />
Für move müsste die Zuweisung</p>
<pre><code class="language-cpp">val = std::move(s.val);
</code></pre>
<p>lauten. s ist <strong>keine</strong> rvalue-Referenz (es hat einen Namen).</p>
<p>Deine Implementierung an sich ist fragwürdig;<br />
- define für eine Konstante<br />
- wozu überhaupt diese Konstante, std::vector verwaltet speicher dynamisch</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2397908</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397908</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Wed, 07 May 2014 13:40:31 GMT</pubDate></item><item><title><![CDATA[Reply to move assignment operator on Wed, 07 May 2014 14:05:03 GMT]]></title><description><![CDATA[<p>so besser? das std::move in der main ist ok?</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;algorithm&gt;
#include &lt;cassert&gt;
#include &lt;array&gt;
using namespace std;

#define SIZE 256

class Stack {
private:
	unsigned int count;

public:
	array&lt;int, SIZE&gt; data;

	Stack(): count(0) {}

	Stack(const Stack &amp;s) {
		std::copy(s.data.begin(), s.data.end(), data.begin());
		count = s.count;
	}

	Stack(const Stack &amp;&amp;s) {
		data = std::move(s.data);
		count = std::move(s.count);
	}

	Stack&amp; operator=(const Stack &amp;s) {
		if(this != &amp;s) {
			std::copy(s.data.begin(), s.data.end(), data.begin());
			count = s.count;
		}

		return *this;
	}

	Stack&amp; operator=(Stack &amp;&amp;s) {
		if(this != &amp;s) {
			data = std::move(s.data);
			count = std::move(s.count);
		}

		return *this;
	}

	void push(int value) {
		if(count &lt; SIZE) {		
			data[count] = value;
			count++;
			return; 
		}

		throw(&quot;stack is full&quot;);
	}	

	int pop() {
		if(count &gt; 0) {
			int element = data[count-1];
			count--;
			return element;
		}

		throw(&quot;stack is empty&quot;);
	}

	bool empty() {
		return count == 0;
	}
};

int main() {
	Stack s;

	s.push(1);
	s.push(2);
	s.push(3);

	Stack a;
	a = s;

	while(!a.empty()) {
		cout &lt;&lt; a.pop() &lt;&lt; endl;
	}

	Stack b;
	b = std::move(s);

	while(!b.empty()) {
		cout &lt;&lt; b.pop() &lt;&lt; endl;
	}

	Stack c(s);
	while(!c.empty()) {
		cout &lt;&lt; c.pop() &lt;&lt; endl;
	}

	return 0;	
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2397916</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397916</guid><dc:creator><![CDATA[niko1]]></dc:creator><pubDate>Wed, 07 May 2014 14:05:03 GMT</pubDate></item><item><title><![CDATA[Reply to move assignment operator on Wed, 07 May 2014 14:30:56 GMT]]></title><description><![CDATA[<p>niko1 schrieb:</p>
<blockquote>
<p>so besser?</p>
</blockquote>
<p>Was erhoffst du dir davon, ein int zu verschieben?<br />
Was liefert empty() des Stacks, von dem du die Daten verschoben hast?</p>
<p>niko1 schrieb:</p>
<blockquote>
<p>das std::move in der main ist ok?</p>
</blockquote>
<p>std::move ist immer ok! Aber was passiert später bei</p>
<pre><code class="language-cpp">Stack c(s);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2397927</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397927</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Wed, 07 May 2014 14:30:56 GMT</pubDate></item><item><title><![CDATA[Reply to move assignment operator on Wed, 07 May 2014 14:35:27 GMT]]></title><description><![CDATA[<p>wenn die daten verschoben werden, dann ist der stack natuerlich empty.<br />
muss den counter noch ruecksetzen...willst du auf das raus?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2397928</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397928</guid><dc:creator><![CDATA[niko1]]></dc:creator><pubDate>Wed, 07 May 2014 14:35:27 GMT</pubDate></item><item><title><![CDATA[Reply to move assignment operator on Wed, 07 May 2014 14:51:10 GMT]]></title><description><![CDATA[<p>niko1 schrieb:</p>
<blockquote>
<p>wenn die daten verschoben werden, dann ist der stack natuerlich empty.<br />
muss den counter noch ruecksetzen...willst du auf das raus?</p>
</blockquote>
<p>Ja. Trotzdem ist dein Stack danach im Eimer, du kannst ihn nicht mehr benutzen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2397934</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397934</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Wed, 07 May 2014 14:51:10 GMT</pubDate></item><item><title><![CDATA[Reply to move assignment operator on Wed, 07 May 2014 15:00:22 GMT]]></title><description><![CDATA[<p>d.h. der move assignment operator macht hier keinen sinn?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2397939</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397939</guid><dc:creator><![CDATA[niko1]]></dc:creator><pubDate>Wed, 07 May 2014 15:00:22 GMT</pubDate></item><item><title><![CDATA[Reply to move assignment operator on Wed, 07 May 2014 15:16:38 GMT]]></title><description><![CDATA[<p>niko1 schrieb:</p>
<blockquote>
<p>d.h. der move assignment operator macht hier keinen sinn?</p>
</blockquote>
<p>Was ist <em>hier</em>?<br />
Deine main-Funktion ist sinnlos, aber für eine Funktion die einen Stack füllt und dann zurück liefert könnte es sinnvoll sein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2397941</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397941</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Wed, 07 May 2014 15:16:38 GMT</pubDate></item><item><title><![CDATA[Reply to move assignment operator on Wed, 07 May 2014 15:42:43 GMT]]></title><description><![CDATA[<p>kann jemand ein beispiel posten wo der move assignment operator / move constructor sinn macht?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2397944</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397944</guid><dc:creator><![CDATA[niko1]]></dc:creator><pubDate>Wed, 07 May 2014 15:42:43 GMT</pubDate></item><item><title><![CDATA[Reply to move assignment operator on Wed, 07 May 2014 16:12:31 GMT]]></title><description><![CDATA[<p>niko1 schrieb:</p>
<blockquote>
<p>kann jemand ein beispiel posten wo der move assignment operator / move constructor sinn macht?</p>
</blockquote>
<p>Davon gibt es bestimmt genug, dass du welche im Netz findest. Das #1-Beispiel ist sicherlich ein Smart-Pointer wie <code>unique_ptr</code> .</p>
<p>Siehe <a href="http://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.4/a01404.html" rel="nofollow">hier</a>, die Zeilen 119-147.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2397950</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397950</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Wed, 07 May 2014 16:12:31 GMT</pubDate></item><item><title><![CDATA[Reply to move assignment operator on Wed, 07 May 2014 18:44:13 GMT]]></title><description><![CDATA[<pre><code>Stack(Stack&amp;&amp; other)
    : count(other.count)
    , data(std::move(other.data))
{
    other.count = 0;
}

Stack&amp; operator = (Stack other)
{
    swap(other);
    return *this;
}

void swap(Stack&amp; other)
{
    using std::swap;
    swap(count, other.count);
    swap(data, other.data);
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2397974</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397974</guid><dc:creator><![CDATA[Kellerautomat]]></dc:creator><pubDate>Wed, 07 May 2014 18:44:13 GMT</pubDate></item><item><title><![CDATA[Reply to move assignment operator on Wed, 07 May 2014 22:44:00 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/28420">@Kellerautomat</a>: Warum Copy&amp;Swap und nicht einfach defaulten?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2397992</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397992</guid><dc:creator><![CDATA[Columbo]]></dc:creator><pubDate>Wed, 07 May 2014 22:44:00 GMT</pubDate></item><item><title><![CDATA[Reply to move assignment operator on Wed, 07 May 2014 22:49:49 GMT]]></title><description><![CDATA[<p>update:</p>
<pre><code>#include &lt;iostream&gt; 
#include &lt;algorithm&gt; 
#include &lt;cassert&gt; 
#include &lt;array&gt; 
using namespace std; 

#define SIZE 256 

class Stack { 
private: 
    unsigned int count; 

public: 
    array&lt;int, SIZE&gt; data; 

    Stack(): count(0) {} 

    Stack(const Stack &amp;s) { 
    	cout &lt;&lt; &quot;Stack(const Stack &amp;s) called&quot; &lt;&lt; endl;
        std::copy(s.data.begin(), s.data.end(), data.begin()); 
        count = s.count; 
    } 

    Stack(Stack &amp;&amp;s) {
    	cout &lt;&lt; &quot;Stack(Stack &amp;&amp;s) called&quot; &lt;&lt; endl;
    	data = std::move(s.data); 
        count = std::move(s.count); 
        s.count = 0;
    } 

    Stack&amp; operator=(const Stack &amp;s) { 
    	cout &lt;&lt; &quot;Stack&amp; operator=(const Stack &amp;s) called&quot; &lt;&lt; endl;
        if(this != &amp;s) { 
            std::copy(s.data.begin(), s.data.end(), data.begin()); 
            count = s.count; 
        } 

        return *this; 
    } 

    Stack&amp; operator=(Stack &amp;&amp;s) {
    	cout &lt;&lt; &quot;Stack&amp; operator=(Stack &amp;&amp;s) called&quot; &lt;&lt; endl;
        if(this != &amp;s) { 
   			data = std::move(s.data); 
            count = std::move(s.count); 
            s.count = 0;
        } 

        return *this; 
    } 

    void push(int value) { 
        if(count &lt; SIZE) {      
            data[count] = value; 
            count++; 
            return; 
        } 

        throw(&quot;stack is full&quot;); 
    }   

    int pop() { 
        if(count &gt; 0) { 
            int element = data[count-1]; 
            count--; 
            return element; 
        } 

        throw(&quot;stack is empty&quot;); 
    } 

    bool empty() { 
        return count == 0; 
    } 
}; 

int main() { 
    Stack s; 

    s.push(1); 
    s.push(2); 
    s.push(3); 

    Stack a; 
    a = s;
  	cout &lt;&lt; &quot;empty: &quot; &lt;&lt; s.empty() &lt;&lt; endl;

    while(!a.empty()) { 
        cout &lt;&lt; a.pop() &lt;&lt; endl; 
    } 

    Stack b; 
    b = std::move(s); 
  	cout &lt;&lt; &quot;empty: &quot; &lt;&lt; s.empty() &lt;&lt; endl;

    while(!b.empty()) { 
        cout &lt;&lt; b.pop() &lt;&lt; endl; 
    }

  	Stack x;
  	Stack y(std::move(x));

    return 0;   
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2397993</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397993</guid><dc:creator><![CDATA[niko1]]></dc:creator><pubDate>Wed, 07 May 2014 22:49:49 GMT</pubDate></item><item><title><![CDATA[Reply to move assignment operator on Wed, 07 May 2014 22:53:06 GMT]]></title><description><![CDATA[<p>warum uebergibst du den Stack im copy construktor nicht per referenz? -&gt;</p>
<pre><code>Stack&amp; operator = (Stack other)
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2397994</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2397994</guid><dc:creator><![CDATA[niko1]]></dc:creator><pubDate>Wed, 07 May 2014 22:53:06 GMT</pubDate></item><item><title><![CDATA[Reply to move assignment operator on Thu, 08 May 2014 06:27:59 GMT]]></title><description><![CDATA[<p>Weil der Parameter hier in der Funktion ohnehin kopiert werden muss.<br />
Siehe auch hier etwas ausführlicher.<br />
<a href="https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Copy-and-swap" rel="nofollow">https://en.wikibooks.org/wiki/More_C%2B%2B_Idioms/Copy-and-swap</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2398029</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2398029</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Thu, 08 May 2014 06:27:59 GMT</pubDate></item><item><title><![CDATA[Reply to move assignment operator on Thu, 08 May 2014 07:43:42 GMT]]></title><description><![CDATA[<p>wie kann copy elision and copy-and-swap idiom auf self assignment pruefen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2398041</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2398041</guid><dc:creator><![CDATA[niko1]]></dc:creator><pubDate>Thu, 08 May 2014 07:43:42 GMT</pubDate></item><item><title><![CDATA[Reply to move assignment operator on Thu, 08 May 2014 07:48:46 GMT]]></title><description><![CDATA[<p>Gar nicht. Das ist auch nicht zwingend notwendig.<br />
Wenn du das unbedingt machen willst musst du halt die andere Version nehmen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2398043</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2398043</guid><dc:creator><![CDATA[Braunstein]]></dc:creator><pubDate>Thu, 08 May 2014 07:48:46 GMT</pubDate></item><item><title><![CDATA[Reply to move assignment operator on Tue, 13 May 2014 14:07:38 GMT]]></title><description><![CDATA[<p>ist folgendes ein potentielles problem? der compiler meldet auch keinen fehler, soll das so sein?</p>
<pre><code>Stack s(s);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2398994</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2398994</guid><dc:creator><![CDATA[niko1]]></dc:creator><pubDate>Tue, 13 May 2014 14:07:38 GMT</pubDate></item><item><title><![CDATA[Reply to move assignment operator on Tue, 13 May 2014 14:14:26 GMT]]></title><description><![CDATA[<p>niko1 schrieb:</p>
<blockquote>
<p>ist folgendes ein potentielles problem? der compiler meldet auch keinen fehler, soll das so sein?</p>
<pre><code>Stack s(s);
</code></pre>
</blockquote>
<p>Das Problem existiert, ist aber nur theoretischer Natur.</p>
<pre><code class="language-cpp">int main()
{
  std::string s	= s;
  std::cout &lt;&lt; s; // Programm stürzt ab.
}
</code></pre>
<p>Kein normaler Mensch schreibt sowas.</p>
<p>Selbstzuweisung über operator= kann hingegen viel einfacher vorkommen wegen Aliasing. Beim Kopier<strong>konstruktor</strong> ist das dagegen ein non-issue.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2398996</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2398996</guid><dc:creator><![CDATA[nonissue]]></dc:creator><pubDate>Tue, 13 May 2014 14:14:26 GMT</pubDate></item></channel></rss>