<?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"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: 9.8 &#8212; Overloading the subscript operator</title>
	<atom:link href="http://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/</link>
	<description></description>
	<lastBuildDate>Tue, 07 Feb 2012 12:30:29 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
	<item>
		<title>By: Nathan_OR</title>
		<link>http://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/comment-page-1/#comment-95081</link>
		<dc:creator>Nathan_OR</dc:creator>
		<pubDate>Tue, 28 Dec 2010 19:20:59 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/#comment-95081</guid>
		<description>Great explanation and yes thank you for the &quot;why operator[] returns a reference&quot; portion, it makes it very clear why it must be so when using array[n] as an l-value.  However I am still not clear why operator[] returns a reference when used as an r-value!  It seems that would be equivalent to:

&lt;code&gt;
int x = 1;
int y = 2;
int array[3] = { 0, 0, 0 };

array[0] = x; // okay, per your explanation, we don&#039;t mean to write 0 = 1, rather, we mean [element at index 0] = 1
// but then what about:
x = array[0]; // why does x end up holding the r-value of &quot;0&quot;, instead of the address of the integer array element at index 0?
&lt;/code&gt;

Perhaps I need more coffee, but the reasoning here is escaping me... is it simply a matter of compiler convention that a reference used as an r-value will always be evaluated for its r-value rather than the l-value?</description>
		<content:encoded><![CDATA[<p>Great explanation and yes thank you for the &#8220;why operator[] returns a reference&#8221; portion, it makes it very clear why it must be so when using array[n] as an l-value.  However I am still not clear why operator[] returns a reference when used as an r-value!  It seems that would be equivalent to:</p>
<p><code><br />
int x = 1;<br />
int y = 2;<br />
int array[3] = { 0, 0, 0 };</p>
<p>array[0] = x; // okay, per your explanation, we don&#039;t mean to write 0 = 1, rather, we mean [element at index 0] = 1<br />
// but then what about:<br />
x = array[0]; // why does x end up holding the r-value of &quot;0&quot;, instead of the address of the integer array element at index 0?<br />
</code></p>
<p>Perhaps I need more coffee, but the reasoning here is escaping me&#8230; is it simply a matter of compiler convention that a reference used as an r-value will always be evaluated for its r-value rather than the l-value?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: codingfreak</title>
		<link>http://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/comment-page-1/#comment-93644</link>
		<dc:creator>codingfreak</dc:creator>
		<pubDate>Wed, 13 Oct 2010 12:25:47 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/#comment-93644</guid>
		<description>I agree guys</description>
		<content:encoded><![CDATA[<p>I agree guys</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: C++ Tutorial and Online Ebook</title>
		<link>http://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/comment-page-1/#comment-87413</link>
		<dc:creator>C++ Tutorial and Online Ebook</dc:creator>
		<pubDate>Wed, 30 Jun 2010 01:00:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/#comment-87413</guid>
		<description>[...] 9.8 Overloading the subscript operator [...]</description>
		<content:encoded><![CDATA[<p>[...] 9.8 Overloading the subscript operator [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jeffrey Bosboom</title>
		<link>http://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/comment-page-1/#comment-87394</link>
		<dc:creator>Jeffrey Bosboom</dc:creator>
		<pubDate>Wed, 30 Jun 2010 00:15:08 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/#comment-87394</guid>
		<description>Your operator[] function will do whatever you write in its body -- it&#039;s just a regular member function with a funny name.  So you can overload operator[] to return an element from the first list or the second list.  You can also do something more complicated like:

&lt;pre&gt;
int&amp; IntList::operator[](const int index)
{
    if (index &lt; 10)
        return m_anList_A[index];
    else if (index &lt; 20)
        return m_anList_B[index-10];
    else
        throw std::out_of_range(&quot;Index out of range&quot;);
}
&lt;!--formatted--&gt;&lt;/pre&gt;

This will return an element from the first list if the index is 0-9, return an element from the second list if the index is 10-19, or throw an exception otherwise.</description>
		<content:encoded><![CDATA[<p>Your operator[] function will do whatever you write in its body &#8212; it&#8217;s just a regular member function with a funny name.  So you can overload operator[] to return an element from the first list or the second list.  You can also do something more complicated like:</p>
<pre>
int&amp; IntList::operator[](const int index)
{
    if (index &lt; 10)
        return m_anList_A[index];
    else if (index &lt; 20)
        return m_anList_B[index-10];
    else
        throw std::out_of_range(&quot;Index out of range&quot;);
}
<!--formatted--></pre>
<p>This will return an element from the first list if the index is 0-9, return an element from the second list if the index is 10-19, or throw an exception otherwise.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: rob</title>
		<link>http://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/comment-page-1/#comment-86002</link>
		<dc:creator>rob</dc:creator>
		<pubDate>Sat, 05 Jun 2010 16:33:57 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/#comment-86002</guid>
		<description>Hi Alex,

In a case where we have 2 lists, can we still overload the [] operator?
How does the overload function know which list to use when returning the reference?

---rob

&lt;pre&gt;

class IntList
{
private:
    int m_anList_A[10];
    int m_anList_B[10];

public:
    int&amp; operator[] (const int nIndex);
};

int&amp; IntList::operator[] (const int nIndex)
{
    return ???
}

&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Hi Alex,</p>
<p>In a case where we have 2 lists, can we still overload the [] operator?<br />
How does the overload function know which list to use when returning the reference?</p>
<p>&#8212;rob</p>
<pre>

class IntList
{
private:
    int m_anList_A[10];
    int m_anList_B[10];

public:
    int&amp; operator[] (const int nIndex);
};

int&amp; IntList::operator[] (const int nIndex)
{
    return ???
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mojito</title>
		<link>http://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/comment-page-1/#comment-80923</link>
		<dc:creator>Mojito</dc:creator>
		<pubDate>Fri, 19 Mar 2010 09:28:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/#comment-80923</guid>
		<description>Hi, and thanks for this post.

I have another question. I want to do sub-indexing too, something like: MyNewType[a][b]. 

anybody? :)</description>
		<content:encoded><![CDATA[<p>Hi, and thanks for this post.</p>
<p>I have another question. I want to do sub-indexing too, something like: MyNewType[a][b]. </p>
<p>anybody? :)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Operator overloading - CodeCall Programming Forum</title>
		<link>http://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/comment-page-1/#comment-78225</link>
		<dc:creator>Operator overloading - CodeCall Programming Forum</dc:creator>
		<pubDate>Fri, 12 Feb 2010 14:14:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/#comment-78225</guid>
		<description>[...] also be able to get cout to work too. I directed you to Learn C++, which has a specific section on overloading the subscript operator that should shed some light on it.     __________________ On [...]</description>
		<content:encoded><![CDATA[<p>[...] also be able to get cout to work too. I directed you to Learn C++, which has a specific section on overloading the subscript operator that should shed some light on it.     __________________ On [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ekta namdeo</title>
		<link>http://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/comment-page-1/#comment-74646</link>
		<dc:creator>ekta namdeo</dc:creator>
		<pubDate>Tue, 22 Dec 2009 04:00:16 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/#comment-74646</guid>
		<description>subs script operator  overloading in c++ with header file include simple and sweet program dijiye</description>
		<content:encoded><![CDATA[<p>subs script operator  overloading in c++ with header file include simple and sweet program dijiye</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Thom</title>
		<link>http://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/comment-page-1/#comment-71849</link>
		<dc:creator>Thom</dc:creator>
		<pubDate>Fri, 06 Nov 2009 17:13:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/#comment-71849</guid>
		<description>I agree.  The concept of references finally clicked when I read that.</description>
		<content:encoded><![CDATA[<p>I agree.  The concept of references finally clicked when I read that.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: davidv</title>
		<link>http://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/comment-page-1/#comment-27506</link>
		<dc:creator>davidv</dc:creator>
		<pubDate>Tue, 16 Sep 2008 05:17:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/98-overloading-the-subscript-operator/#comment-27506</guid>
		<description>The &quot;Why operator[] returns a reference&quot; part was brilliant. You made a pretty difficult idea (at least to me) to seem obvious.</description>
		<content:encoded><![CDATA[<p>The &#8220;Why operator[] returns a reference&#8221; part was brilliant. You made a pretty difficult idea (at least to me) to seem obvious.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

