<?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[this Zeiger]]></title><description><![CDATA[<p>huhu ihrs,</p>
<p>den this Zeiger kann man ja nicht verbiegen.<br />
Wenn ich aber z.B. folgendes in einer Fkt der Klasse &quot;Foo&quot;mache:</p>
<pre><code class="language-cpp">...
Foo f;
this = &amp;f;
...
</code></pre>
<p>dann bringt der Compiler folgenden Fehler:<br />
Error: non-lvalue in assignment.</p>
<p>Ich dachte aber der this Zeiger ist konstant und hätte somit eigentlich<br />
dies erwartet:<br />
Error: assignment of read-only variable `this'</p>
<p>Nun meine Frage: Ist der this Zeiger also doch nicht konstant?</p>
<p>thx schonmal</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/256765/this-zeiger</link><generator>RSS for Node</generator><lastBuildDate>Fri, 11 Sep 2026 04:39:49 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/256765.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 17 Dec 2009 15:53:43 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to this Zeiger on Thu, 17 Dec 2009 15:53:43 GMT]]></title><description><![CDATA[<p>huhu ihrs,</p>
<p>den this Zeiger kann man ja nicht verbiegen.<br />
Wenn ich aber z.B. folgendes in einer Fkt der Klasse &quot;Foo&quot;mache:</p>
<pre><code class="language-cpp">...
Foo f;
this = &amp;f;
...
</code></pre>
<p>dann bringt der Compiler folgenden Fehler:<br />
Error: non-lvalue in assignment.</p>
<p>Ich dachte aber der this Zeiger ist konstant und hätte somit eigentlich<br />
dies erwartet:<br />
Error: assignment of read-only variable `this'</p>
<p>Nun meine Frage: Ist der this Zeiger also doch nicht konstant?</p>
<p>thx schonmal</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1824047</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1824047</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Thu, 17 Dec 2009 15:53:43 GMT</pubDate></item><item><title><![CDATA[Reply to this Zeiger on Thu, 17 Dec 2009 15:59:25 GMT]]></title><description><![CDATA[<p>Der Standard verlangt AFAIR vom Compiler nicht, daß Du bei fehlerhaftem Code bestimmte Fehlermeldungen bekommst. Wenn es den Compilerbauern einfacher war, this mit viel Comilermagie zu basteln, statt als schnöden const-Zeiger, ...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1824051</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1824051</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Thu, 17 Dec 2009 15:59:25 GMT</pubDate></item><item><title><![CDATA[Reply to this Zeiger on Thu, 17 Dec 2009 16:10:23 GMT]]></title><description><![CDATA[<p>Was gibt es an der Fehlermeldung auszusetzen? this ist kein lvalue, und genau das bemängelt der Compiler...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1824055</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1824055</guid><dc:creator><![CDATA[DocShoe]]></dc:creator><pubDate>Thu, 17 Dec 2009 16:10:23 GMT</pubDate></item><item><title><![CDATA[Reply to this Zeiger on Thu, 17 Dec 2009 16:11:20 GMT]]></title><description><![CDATA[<p>Der this Zeiger ist gar keine Variable, wie du fälschlicherweise annimmst. Deswegen bekommst du auch nicht die Fehlermeldung die du erwartest. Die Meldung, dass es sich nicht um einen lvalue handelt, bedeutet aber praktisch das gleiche: es ist kein Objekt dem man etwas zuweisen kann<sup>*</sup>.</p>
<p><sup>*</sup>: Die betonung liegt hier auf &quot;kann&quot;. Konstanten Variablen haben manchmal eine echte Speicheradresse (sofern nicht wegoptimiert). Man könnte ihnen theoretisch was zuweisen, der Compiler verhindert es aber. Der this-Zeiger ist gar kein Objekt, das man adressieren könnte, sondern einfach ein Schlüsselwort der Sprache das für &quot;Adresse des aktuellen Objekts&quot; steht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1824056</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1824056</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Thu, 17 Dec 2009 16:11:20 GMT</pubDate></item><item><title><![CDATA[Reply to this Zeiger on Thu, 17 Dec 2009 17:15:28 GMT]]></title><description><![CDATA[<p>thx @ SeppJ, das hilft mir weiter <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1824084</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1824084</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Thu, 17 Dec 2009 17:15:28 GMT</pubDate></item><item><title><![CDATA[Reply to this Zeiger on Thu, 17 Dec 2009 18:34:40 GMT]]></title><description><![CDATA[<blockquote>
<p>Die betonung liegt hier auf &quot;kann&quot;. Konstanten Variablen haben manchmal eine echte Speicheradresse (sofern nicht wegoptimiert). Man könnte ihnen theoretisch was zuweisen, der Compiler verhindert es aber. Der this-Zeiger ist gar kein Objekt, das man adressieren könnte, sondern einfach ein Schlüsselwort der Sprache das für &quot;Adresse des aktuellen Objekts&quot; steht.</p>
</blockquote>
<p>Das ist falsch.</p>
<blockquote>
<p>9.3.2 The this pointer [class.this]<br />
1 In the body of a nonstatic (9.3) member function, the keyword this is a non-lvalue expression whose<br />
value is the address of the object for which the function is called. The type of this in a member function<br />
of a class X is X*. If the member function is declared const, the type of this is const X*, if the member<br />
function is declared volatile, the type of this is volatile X*, and if the member function is<br />
declared const volatile, the type of this is const volatile X*.</p>
</blockquote>
<p>this ist also ein ganz normaler Zeiger vom Typ const T*. (je nachdem noch der der volatile Bezeichner.)</p>
<p>Somit verhält sich this, wie jeder andere const Zeiger ebenfalls.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1824111</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1824111</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Thu, 17 Dec 2009 18:34:40 GMT</pubDate></item><item><title><![CDATA[Reply to this Zeiger on Thu, 17 Dec 2009 18:42:11 GMT]]></title><description><![CDATA[<p>drakon schrieb:</p>
<blockquote>
<blockquote>
<p>9.3.2 The this pointer [class.this]<br />
1 In the body of a nonstatic (9.3) member function, the keyword this is a non-lvalue expression whose<br />
value is the address of the object for which the function is called. The type of this in a member function<br />
of a class X is X*. If the member function is declared const, the type of this is const X*, if the member<br />
function is declared volatile, the type of this is volatile X*, and if the member function is<br />
declared const volatile, the type of this is const volatile X*.</p>
</blockquote>
<p>this ist also ein ganz normaler Zeiger vom Typ const T*. (je nachdem noch der der volatile Bezeichner.)</p>
<p>Somit verhält sich this, wie jeder andere const Zeiger ebenfalls.</p>
</blockquote>
<p>In deiner zitierten Textstelle steht aber was ganz anderes? <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="😕"
    /><br />
Außerdem beschreibt &quot;const T*&quot; den Typ eines (veränderlichen) Zeigers auf ein konstantes T und nicht den Typ eines konstanter Zeigers auf ein (veränderliches) T.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1824112</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1824112</guid><dc:creator><![CDATA[life]]></dc:creator><pubDate>Thu, 17 Dec 2009 18:42:11 GMT</pubDate></item><item><title><![CDATA[Reply to this Zeiger on Thu, 17 Dec 2009 18:43:51 GMT]]></title><description><![CDATA[<p>Hmm. Ich hab da jetzt übereilig da was hingeschrieben. Wenn überhaupt müsste ja der Zeiger const sein. (Das andere ist natürlich klar, wenn das Objekt const ist, dann ist, dann zeigt auch der Zeiger auf ein Konstates Objekt). Aber ich hatte wirklich im Kopf, dass this vom Typ T *const ist.. Das verwirrt mich jetzt doch ein bisschen. Macht ja keinen Sinn this etwas zuweisen zu wollen..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1824116</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1824116</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Thu, 17 Dec 2009 18:43:51 GMT</pubDate></item><item><title><![CDATA[Reply to this Zeiger on Thu, 17 Dec 2009 19:10:08 GMT]]></title><description><![CDATA[<p>drakon schrieb:</p>
<blockquote>
<blockquote>
<p>9.3.2 The this pointer [class.this]<br />
1 In the body of a nonstatic (9.3) member function, the keyword this is a non-lvalue expression whose<br />
value is the address of the object for which the function is called. The type of this in a member function<br />
of a class X is X*. If the member function is declared const, the type of this is const X*, if the member<br />
function is declared volatile, the type of this is volatile X*, and if the member function is<br />
declared const volatile, the type of this is const volatile X*.</p>
</blockquote>
<p>this ist also ein ganz normaler Zeiger vom Typ const T*. (je nachdem noch der der volatile Bezeichner.)</p>
<p>Somit verhält sich this, wie jeder andere const Zeiger ebenfalls.</p>
</blockquote>
<p>Also den Text aus dem aktuellen Draft des Standards kann ich nur bestätigen, hab' da mal reingesehen, und Drakon's Post ist mit diesem Inhaltsgleich.</p>
<p>Mein Visual Studio zeigt mir für 'this' auch bei folgender Klasse die in dem Text beschriebenen Datentypen an, somit wurde es dort auch so implementiert:</p>
<pre><code class="language-cpp">class CThis
{
public:
     int i;

     void CV-Unqualified(void)
     {
          int a = this-&gt;i;     // CThis *const this
          // Konstanter Zeiger (rvalue) auf CThis
     };

     void C-Qualified(void) const
     {
          int a = this-&gt;i;     // const CThis *const this
          // Konstanter Zeiger (rvalue) auf konstantes CThis
     };

     void V-Qualified(void) volatile
     {
          int a = this-&gt;i;     // volatile CThis *const this
          // Konstanter Zeiger (rvalue) auf volatile CThis
     };

     void CV-Qualified(void) const volatile
     {
          int a = this-&gt;i;     // volatile const CThis *const this
          // Konstanter Zeiger (rvalue) auf konstantes volatile CThis
     };
     CThis(){};
     ~CThis(){};

};
</code></pre>
<p>In jedem der Fälle handelt es sich, wie wieder im Std gut beschrieben, um einen non-lvlaue (also ein rvalue), dem ja sowieso nichts zuweisbar ist. Somit, finde ich, passt die Fehlermeldung die DWeb bekommt allemal.</p>
<p>Gruß<br />
PuerNoctis <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1824126</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1824126</guid><dc:creator><![CDATA[PuerNoctis]]></dc:creator><pubDate>Thu, 17 Dec 2009 19:10:08 GMT</pubDate></item><item><title><![CDATA[Reply to this Zeiger on Thu, 17 Dec 2009 19:08:52 GMT]]></title><description><![CDATA[<p>Ja, passen tuts allemal. Mich wundert einfach nur, dass der Zeiger per Standard nicht const sein muss. (non-lvalue verhindert natürlich Zuweisung, aber ich finds trotzdem ein wenig komisch..)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1824127</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1824127</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Thu, 17 Dec 2009 19:08:52 GMT</pubDate></item><item><title><![CDATA[Reply to this Zeiger on Thu, 17 Dec 2009 19:15:33 GMT]]></title><description><![CDATA[<p>Hm, vielleicht hat Microsoft das so implementiert, dass ein non-lvalue im VC nur durch ein const-Verhalten überhaupt existieren kann. Somit würde sich das T*const wohl eher durch den Compiler ergeben, und muss nicht durch den Standard definiert sein. Hab' das jetzt leider nicht mit dem GCC geprüft oder anderen Compilern, ob das bei denen auch T*const ist.</p>
<p>Könnte mir aber wirklich gut vorstellen, dass dem Std egal ist WIE der rvalue entsteht, Hauptsache er ist einer^^. Wäre für mich zumindet eine logische Erklärung...</p>
<p>Gruß<br />
PuerNoctis</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1824131</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1824131</guid><dc:creator><![CDATA[PuerNoctis]]></dc:creator><pubDate>Thu, 17 Dec 2009 19:15:33 GMT</pubDate></item><item><title><![CDATA[Reply to this Zeiger on Thu, 17 Dec 2009 19:24:47 GMT]]></title><description><![CDATA[<p>Oh mein Gott, folgendes funktioniert:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

class CThis
{
public:
     int i;

     void Umbiegen(void)
     {
          CThis Other;
          Other.i = 666;
          (const_cast&lt;CThis*&gt;(this)) = &amp;Other;
          std::cout &lt;&lt; this-&gt;i;
     };

     CThis() : i(0){};
     ~CThis(){};

};

int main(int argc, char** argv)
{
     CThis Test;
     Test.Umbiegen();
     return 0;
}
</code></pre>
<blockquote>
<p>Ausgabe:<br />
666</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/1824135</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1824135</guid><dc:creator><![CDATA[PuerNoctis]]></dc:creator><pubDate>Thu, 17 Dec 2009 19:24:47 GMT</pubDate></item><item><title><![CDATA[Reply to this Zeiger on Thu, 17 Dec 2009 19:25:52 GMT]]></title><description><![CDATA[<p>PuerNoctis schrieb:</p>
<blockquote>
<p>Hm, vielleicht hat Microsoft das so implementiert, dass ein non-lvalue im VC nur durch ein const-Verhalten überhaupt existieren kann.</p>
</blockquote>
<p>Dann würde</p>
<pre><code class="language-cpp">struct foo
{
    void bar() {}
};
int main()
{
    foo().bar();
}
</code></pre>
<p>Ja gar nicht gehen (weil bar non-const und non-lvalue). Oder hab ich dich falsch verstanden?</p>
<p>Bei GCC ist this übrigens kein rvalue, was sich einfach dadurch beweisen lässt, dass folgendes kompiliert:</p>
<pre><code class="language-cpp">template&lt;typename T&gt;
void test(T&amp;){}

struct foo
{
    void bar()
    {
        test(this);
    }
};
</code></pre>
<p>Mit richtigen rvalues kompiliert das nicht. (z.B. test(foo()))</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1824137</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1824137</guid><dc:creator><![CDATA[Ryuzaki]]></dc:creator><pubDate>Thu, 17 Dec 2009 19:25:52 GMT</pubDate></item><item><title><![CDATA[Reply to this Zeiger on Thu, 17 Dec 2009 19:31:14 GMT]]></title><description><![CDATA[<p>Du rufst da einfach eine Funktion auf ein tempotäres Objekt auf.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1824141</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1824141</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Thu, 17 Dec 2009 19:31:14 GMT</pubDate></item><item><title><![CDATA[Reply to this Zeiger on Thu, 17 Dec 2009 19:52:34 GMT]]></title><description><![CDATA[<p>Ryuzaki schrieb:</p>
<blockquote>
<p>Bei GCC ist this übrigens kein rvalue, was sich einfach dadurch beweisen lässt, dass folgendes kompiliert:</p>
<pre><code class="language-cpp">template&lt;typename T&gt;
void test(T&amp;){}

struct foo
{
    void bar()
    {
        test(this);
    }
};
</code></pre>
<p>Mit richtigen rvalues kompiliert das nicht. (z.B. test(foo()))</p>
</blockquote>
<p>Ich fürchte (drakon bestätigend), in T steckt am ende genug const, daß ein temoptäres Objekt gebaut wird.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
using namespace std;

struct foo
{
	static void test(foo*const&amp; th)
	{
	}
	void bar()
	{
			test(this+1);
	}
};

int main() {
	foo f;
	f.bar();
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1824152</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1824152</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Thu, 17 Dec 2009 19:52:34 GMT</pubDate></item><item><title><![CDATA[Reply to this Zeiger on Thu, 17 Dec 2009 21:00:29 GMT]]></title><description><![CDATA[<p>Skalare rvalues sind niemals cv-qualifiziert und zwar einfach deshalb, weil es handelt sich nicht um Objekte handelt, d.h. Speicherbereiche, deren Inhalt man sich als änderbar vorstellen könnte. (3.10/9)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1824208</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1824208</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Thu, 17 Dec 2009 21:00:29 GMT</pubDate></item></channel></rss>