<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	>
<channel>
	<title>Comments on: 8.7 &#8212; The hidden &#8220;this&#8221; pointer</title>
	<atom:link href="http://www.learncpp.com/cpp-tutorial/87-the-hidden-this-pointer/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learncpp.com/cpp-tutorial/87-the-hidden-this-pointer/</link>
	<description></description>
	<pubDate>Fri, 29 Aug 2008 19:48:30 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.5.1</generator>
		<item>
		<title>By: Learn C++ - &#187; 8.6 &#8212; Destructors</title>
		<link>http://www.learncpp.com/cpp-tutorial/87-the-hidden-this-pointer/#comment-14393</link>
		<dc:creator>Learn C++ - &#187; 8.6 &#8212; Destructors</dc:creator>
		<pubDate>Mon, 05 May 2008 01:56:44 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/87-the-hidden-this-pointer/#comment-14393</guid>
		<description>[...] 2007      Prev/Next Posts   &#171; 8.5 &#8212; Constructors &#124; Home &#124; 8.7 &#8212; The hidden &#8220;this&#8221; pointer &#187;     Thursday, September 6th, 2007 at 9:14 [...]</description>
		<content:encoded><![CDATA[<p>[...] 2007      Prev/Next Posts   &laquo; 8.5 &#8212; Constructors | Home | 8.7 &#8212; The hidden &#8220;this&#8221; pointer &raquo;     Thursday, September 6th, 2007 at 9:14 [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.learncpp.com/cpp-tutorial/87-the-hidden-this-pointer/#comment-10291</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Mon, 24 Mar 2008 22:20:48 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/87-the-hidden-this-pointer/#comment-10291</guid>
		<description>I don't see anything syntactically wrong with that program, so without knowing what the error message was, I can't say.  It is bizarre that you seem to have SetID() declared as a non-member function.  Really it would be better if SetID() were declared as a member function, and then you could just say:

&lt;pre&gt;
cSimple.SetID(2);
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>I don&#8217;t see anything syntactically wrong with that program, so without knowing what the error message was, I can&#8217;t say.  It is bizarre that you seem to have SetID() declared as a non-member function.  Really it would be better if SetID() were declared as a member function, and then you could just say:</p>
<pre>
cSimple.SetID(2);
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: emre</title>
		<link>http://www.learncpp.com/cpp-tutorial/87-the-hidden-this-pointer/#comment-10186</link>
		<dc:creator>emre</dc:creator>
		<pubDate>Sun, 23 Mar 2008 11:36:51 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/87-the-hidden-this-pointer/#comment-10186</guid>
		<description>I tried this but didn't work , why ?

&lt;pre&gt;
int main()
{
    Simple cSimple(1);
    SetID(&#038;cSimple,2);
    std::cout &lt;&lt; cSimple.GetID() &lt;&lt; std::endl;
}
</description>
		<content:encoded><![CDATA[<p>I tried this but didn&#8217;t work , why ?</p>
<pre>
int main()
{
    Simple cSimple(1);
    SetID(&#038;cSimple,2);
    std::cout << cSimple.GetID() << std::endl;
}</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zafer</title>
		<link>http://www.learncpp.com/cpp-tutorial/87-the-hidden-this-pointer/#comment-7278</link>
		<dc:creator>Zafer</dc:creator>
		<pubDate>Tue, 05 Feb 2008 13:07:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/87-the-hidden-this-pointer/#comment-7278</guid>
		<description>Ok thanks.</description>
		<content:encoded><![CDATA[<p>Ok thanks.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Alex</title>
		<link>http://www.learncpp.com/cpp-tutorial/87-the-hidden-this-pointer/#comment-7256</link>
		<dc:creator>Alex</dc:creator>
		<pubDate>Tue, 05 Feb 2008 03:31:37 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/87-the-hidden-this-pointer/#comment-7256</guid>
		<description>As you note, the "this" pointer can contain different addresses so it can point to different objects.  This does not violate the fact that the *this pointer is const.  All the const means is that we can not change what the *this pointer points to when we're inside the function.  The const does not restrict what we set the pointer to in the first place!

For example, consider the following function:

&lt;pre&gt;
int PrintValue(const int nValue)
{
    // We can not change the value of nValue inside the function!
    cout &lt;&lt; nValue;
}

int main()
{
    // However, we can set nValue to whatever we want
    // when we call the function!
    PrintValue(4);
    PrintValue(6);
}
&lt;/pre&gt;

This program is pretty straightforward.  The first time we call PrintValue, we set nValue to 4.  The second time we set it to 6.

The *this pointer works exactly like this, except that it's type is "X* const" instead of int (where X is the name of the class the non-static member function belongs to), and the compiler automatically sets it's value based on the object we're calling the function on.</description>
		<content:encoded><![CDATA[<p>As you note, the &#8220;this&#8221; pointer can contain different addresses so it can point to different objects.  This does not violate the fact that the *this pointer is const.  All the const means is that we can not change what the *this pointer points to when we&#8217;re inside the function.  The const does not restrict what we set the pointer to in the first place!</p>
<p>For example, consider the following function:</p>
<pre>
int PrintValue(const int nValue)
{
    // We can not change the value of nValue inside the function!
    cout < < nValue;
}

int main()
{
    // However, we can set nValue to whatever we want
    // when we call the function!
    PrintValue(4);
    PrintValue(6);
}
</pre>
<p>This program is pretty straightforward.  The first time we call PrintValue, we set nValue to 4.  The second time we set it to 6.</p>
<p>The *this pointer works exactly like this, except that it&#8217;s type is &#8220;X* const&#8221; instead of int (where X is the name of the class the non-static member function belongs to), and the compiler automatically sets it&#8217;s value based on the object we&#8217;re calling the function on.</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Zafer</title>
		<link>http://www.learncpp.com/cpp-tutorial/87-the-hidden-this-pointer/#comment-7249</link>
		<dc:creator>Zafer</dc:creator>
		<pubDate>Tue, 05 Feb 2008 02:03:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/87-the-hidden-this-pointer/#comment-7249</guid>
		<description>As a pointer, "this" contains the memory address of a class object. However, since we can call a class function from different objects, "this" pointer should be able to contain different addresses so that it can point to different objects. How does this happen when "this" is a const pointer?</description>
		<content:encoded><![CDATA[<p>As a pointer, &#8220;this&#8221; contains the memory address of a class object. However, since we can call a class function from different objects, &#8220;this&#8221; pointer should be able to contain different addresses so that it can point to different objects. How does this happen when &#8220;this&#8221; is a const pointer?</p>
]]></content:encoded>
	</item>
</channel>
</rss>
