<?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[copy assigment und object construction in einer Zeile]]></title><description><![CDATA[<p>Wenn ich folgende Zeilen compiliere erwarte ich eigentlich, dass<br />
1. der default constructor der Klasse MyObject aufgerufen wird.<br />
2. der assignment operator der Klasse MyObject aufgerufen wird.<br />
Die Zeilen sind:<br />
MyObject mos;<br />
MyObject moc=mos;</p>
<p>Was wirklich passiert, ist, dass weder der assignment operator noch der default constructor aufgerufen wird, moc.i aber mit 0 initialisiert ist. Soll das so sein oder mach ich was falsch? Compiliert wurde das ganze mit Visual Studio 2008.</p>
<p>Hier ist der gesamte code:</p>
<pre><code class="language-cpp">#include &quot;stdafx.h&quot;
#include &lt;iostream&gt;

using namespace std;

class MyObject{
public:
	MyObject():i(0)
	{
		cout&lt;&lt;&quot;Default constructor called\n&quot;;
	}

	MyObject&amp; operator=(const MyObject&amp; object)
	{
			cout&lt;&lt;&quot;Object internal assignment operator called\n&quot;;
			this-&gt;i=object.i;
			return *this;
	}

	int i;

};

int _tmain(int argc, _TCHAR* argv[])
{

	cout&lt;&lt;&quot;Creating object on Stack\n&quot;;
	MyObject mov;
        mov.i=1;
	cout&lt;&lt;&quot;Creating reference to object\n&quot;;
	MyObject &amp; mor=mov;
	cout&lt;&lt;&quot;Creating pointer to object\n&quot;;
	MyObject * mop=&amp;mor;
	cout&lt;&lt;&quot;Creating copy of object from original\n&quot;;
	MyObject mocv=mov;//neither assignment operator nor default constructor are called. Why?
	cout&lt;&lt;&quot;mocv.i should be 1 is &quot;&lt;&lt;mocv.i&lt;&lt;&quot;\n&quot;;//outputs 0
	cout&lt;&lt;&quot;Creating 2 objects on stack, assigning one to the other\n&quot;;
	MyObject mov1;
	MyObject mov2;
	mov1=mov2;//assignment operator is called

	cout&lt;&lt;&quot;Creating object on heap\n&quot;;
	MyObject * mooh/*my object on heap*/=new MyObject();
	cout&lt;&lt;&quot;Copy assigning heap object to stack object\n&quot;;
	mov=*mooh;//assignment operator is called
	cout&lt;&lt;&quot;Copy assigning stack object to heap object\n&quot;;
	cout&lt;&lt;&quot;Creating second object on heap\n&quot;;
	MyObject * mooh2=new MyObject();
	cout&lt;&lt;&quot;Copy assigning first object on heap to second object on heap\n&quot;;
	*mooh2=*mooh;//assignment operator called;
        return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/215711/copy-assigment-und-object-construction-in-einer-zeile</link><generator>RSS for Node</generator><lastBuildDate>Mon, 12 Oct 2026 02:08:32 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/215711.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 14 Jun 2008 12:45:56 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to copy assigment und object construction in einer Zeile on Sat, 14 Jun 2008 12:45:56 GMT]]></title><description><![CDATA[<p>Wenn ich folgende Zeilen compiliere erwarte ich eigentlich, dass<br />
1. der default constructor der Klasse MyObject aufgerufen wird.<br />
2. der assignment operator der Klasse MyObject aufgerufen wird.<br />
Die Zeilen sind:<br />
MyObject mos;<br />
MyObject moc=mos;</p>
<p>Was wirklich passiert, ist, dass weder der assignment operator noch der default constructor aufgerufen wird, moc.i aber mit 0 initialisiert ist. Soll das so sein oder mach ich was falsch? Compiliert wurde das ganze mit Visual Studio 2008.</p>
<p>Hier ist der gesamte code:</p>
<pre><code class="language-cpp">#include &quot;stdafx.h&quot;
#include &lt;iostream&gt;

using namespace std;

class MyObject{
public:
	MyObject():i(0)
	{
		cout&lt;&lt;&quot;Default constructor called\n&quot;;
	}

	MyObject&amp; operator=(const MyObject&amp; object)
	{
			cout&lt;&lt;&quot;Object internal assignment operator called\n&quot;;
			this-&gt;i=object.i;
			return *this;
	}

	int i;

};

int _tmain(int argc, _TCHAR* argv[])
{

	cout&lt;&lt;&quot;Creating object on Stack\n&quot;;
	MyObject mov;
        mov.i=1;
	cout&lt;&lt;&quot;Creating reference to object\n&quot;;
	MyObject &amp; mor=mov;
	cout&lt;&lt;&quot;Creating pointer to object\n&quot;;
	MyObject * mop=&amp;mor;
	cout&lt;&lt;&quot;Creating copy of object from original\n&quot;;
	MyObject mocv=mov;//neither assignment operator nor default constructor are called. Why?
	cout&lt;&lt;&quot;mocv.i should be 1 is &quot;&lt;&lt;mocv.i&lt;&lt;&quot;\n&quot;;//outputs 0
	cout&lt;&lt;&quot;Creating 2 objects on stack, assigning one to the other\n&quot;;
	MyObject mov1;
	MyObject mov2;
	mov1=mov2;//assignment operator is called

	cout&lt;&lt;&quot;Creating object on heap\n&quot;;
	MyObject * mooh/*my object on heap*/=new MyObject();
	cout&lt;&lt;&quot;Copy assigning heap object to stack object\n&quot;;
	mov=*mooh;//assignment operator is called
	cout&lt;&lt;&quot;Copy assigning stack object to heap object\n&quot;;
	cout&lt;&lt;&quot;Creating second object on heap\n&quot;;
	MyObject * mooh2=new MyObject();
	cout&lt;&lt;&quot;Copy assigning first object on heap to second object on heap\n&quot;;
	*mooh2=*mooh;//assignment operator called;
        return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1528941</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1528941</guid><dc:creator><![CDATA[OrangeOwl]]></dc:creator><pubDate>Sat, 14 Jun 2008 12:45:56 GMT</pubDate></item><item><title><![CDATA[Reply to copy assigment und object construction in einer Zeile on Sat, 14 Jun 2008 12:55:41 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">MyObject mocv=mov;
</code></pre>
<p>Hier würde ich darauf tippen, dass der copy-ctor aufgerufen wird, weil das Compiler das optimieren kann. Sonst hättest du ja eine Konstruktion und dann eine Zuweisung. Das kannst du mit dem copy-ctor kürzer haben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1528950</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1528950</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Sat, 14 Jun 2008 12:55:41 GMT</pubDate></item><item><title><![CDATA[Reply to copy assigment und object construction in einer Zeile on Sat, 14 Jun 2008 13:16:06 GMT]]></title><description><![CDATA[<p>Danke für den Tip, ist Tatsächlich so. Habe einen copy constructor hinzugefügt und der wurde aufgerufen. Sind copy assignment und copy construction richtig implementiert macht das natürlich Sinn, da es viel schneller ausgeführt wird. Muss mal ausprobieren welche Compiler Switches dafür verantwortlich sind. Eigentlich bin ich ja nicht glücklich darüber, dass der Compiler so eigenmächtig in meinen Code eingreift. Ist ja nicht so, dass das selbe Ergebnis entstehen würde.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1528966</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1528966</guid><dc:creator><![CDATA[OrangeOwl]]></dc:creator><pubDate>Sat, 14 Jun 2008 13:16:06 GMT</pubDate></item><item><title><![CDATA[Reply to copy assigment und object construction in einer Zeile on Sat, 14 Jun 2008 13:23:45 GMT]]></title><description><![CDATA[<p>das X x = y ist niemals eine zuweisung, sondern immer eine initialisierung. das hat nichts mit optimierungen von compilern zu tun, es ist schlicht und einfach so festgelegt, dass hier der copy-ctor verwendet wird (in gewissen situationen darf der compiler hier allerdings den tatsächlichen aufruf des copty-ctors wegoptimieren).</p>
<p>stichwörter zum googlen: copy-initialization und direct-initialization</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1528973</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1528973</guid><dc:creator><![CDATA[queer_boy]]></dc:creator><pubDate>Sat, 14 Jun 2008 13:23:45 GMT</pubDate></item><item><title><![CDATA[Reply to copy assigment und object construction in einer Zeile on Sat, 14 Jun 2008 13:24:15 GMT]]></title><description><![CDATA[<p>OrangeOwl schrieb:</p>
<blockquote>
<p>Danke für den Tip, ist Tatsächlich so. Habe einen copy constructor hinzugefügt und der wurde aufgerufen. Sind copy assignment und copy construction richtig implementiert macht das natürlich Sinn, da es viel schneller ausgeführt wird. Muss mal ausprobieren welche Compiler Switches dafür verantwortlich sind. Eigentlich bin ich ja nicht glücklich darüber, dass der Compiler so eigenmächtig in meinen Code eingreift. Ist ja nicht so, dass das selbe Ergebnis entstehen würde.</p>
</blockquote>
<p>Doch, wenn du es richtig machst, darf das keine Rolle spielen, was aufgerufen wird. Darum wird von denen ja von den <a href="http://tutorial.schornboeck.net/bigthree.htm" rel="nofollow">&quot;Grossen 3&quot;</a> gesprochen, das heisst, wenn du einen assignment-ctor, copy-ctor oder d-tor brauchst, brauchst du auch die anderen 2.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1528974</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1528974</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Sat, 14 Jun 2008 13:24:15 GMT</pubDate></item><item><title><![CDATA[Reply to copy assigment und object construction in einer Zeile on Sat, 14 Jun 2008 14:10:18 GMT]]></title><description><![CDATA[<p>Die Regel kannte ich nicht bzw. war mir nicht klar was ihre Missachtung für folgen haben kann. Also, danke nochmal, auch für diesen Hinweis.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1529003</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1529003</guid><dc:creator><![CDATA[OrangeOwl]]></dc:creator><pubDate>Sat, 14 Jun 2008 14:10:18 GMT</pubDate></item></channel></rss>