<?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: 8.14 &#8212; Anonymous variables and objects</title>
	<atom:link href="http://www.learncpp.com/cpp-tutorial/814-anonymous-variables-and-objects/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.learncpp.com/cpp-tutorial/814-anonymous-variables-and-objects/</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: smitha</title>
		<link>http://www.learncpp.com/cpp-tutorial/814-anonymous-variables-and-objects/comment-page-1/#comment-95556</link>
		<dc:creator>smitha</dc:creator>
		<pubDate>Tue, 14 Jun 2011 05:43:35 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/814-anonymous-variables-and-objects/#comment-95556</guid>
		<description>got a doubt here.  Consider the same class.&lt;pre&gt;
class Cents  
{  
 private:  
    int m_nCents;  

 public:  

     Cents(int nCents) { m_nCents = nCents; }  
     int GetCents(Cents obj) { obj.m_nCents =10;}  

 };  
int main()
{
  Cents c1,c2;
   c1.GetCents(c2);
}

If you consider the function GetCents, am accessing the private variable of technically a different object namely obj. Why am I allowed to do this? Does not this override the quality that the private member of a class cannot be accessed by outside code?

Regards,
Smitha</description>
		<content:encoded><![CDATA[<p>got a doubt here.  Consider the same class.
<pre>
class Cents
{
 private:
    int m_nCents;  

 public:  

     Cents(int nCents) { m_nCents = nCents; }
     int GetCents(Cents obj) { obj.m_nCents =10;}  

 };
int main()
{
  Cents c1,c2;
   c1.GetCents(c2);
}

If you consider the function GetCents, am accessing the private variable of technically a different object namely obj. Why am I allowed to do this? Does not this override the quality that the private member of a class cannot be accessed by outside code?

Regards,
Smitha</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: mslade</title>
		<link>http://www.learncpp.com/cpp-tutorial/814-anonymous-variables-and-objects/comment-page-1/#comment-94994</link>
		<dc:creator>mslade</dc:creator>
		<pubDate>Thu, 02 Dec 2010 18:24:15 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/814-anonymous-variables-and-objects/#comment-94994</guid>
		<description>Great guide.  I thought I would add some advice for those new to programming:

If you&#039;re using the same expression in anonymous values repeatedly, it should probably be a named variable instead.  For example, if you&#039;re passing strlen(szName) into the value of 3 different functions, capture it in a named variable and use that three times, instead.

This way, strlen(szName) doesn&#039;t have to be calculated three times.  It also makes the code more compact and thus more readable.</description>
		<content:encoded><![CDATA[<p>Great guide.  I thought I would add some advice for those new to programming:</p>
<p>If you&#8217;re using the same expression in anonymous values repeatedly, it should probably be a named variable instead.  For example, if you&#8217;re passing strlen(szName) into the value of 3 different functions, capture it in a named variable and use that three times, instead.</p>
<p>This way, strlen(szName) doesn&#8217;t have to be calculated three times.  It also makes the code more compact and thus more readable.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gammerz</title>
		<link>http://www.learncpp.com/cpp-tutorial/814-anonymous-variables-and-objects/comment-page-1/#comment-89624</link>
		<dc:creator>Gammerz</dc:creator>
		<pubDate>Wed, 04 Aug 2010 17:37:25 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/814-anonymous-variables-and-objects/#comment-89624</guid>
		<description>&lt;pre&gt;Cents cCentsSum = Add(cCents1, cCents2);&lt;/pre&gt;
declares an object cCentsSum of type Cents and then initialises it, all on one line. It could have been split into two commands:-
&lt;pre&gt;Cents cCentsSum;
cCentsSum = Add(cCents1, cCents2);&lt;/pre&gt;

The Add function returns an object of type Cents so this initialisation is valid. Note that the Add function is not a member of the class cents, it&#039;s defined outside the class, so is a &quot;normal&quot; function. It&#039;s ok to use functions or expressions as initialisation values, we are not restricted to literals.</description>
		<content:encoded><![CDATA[<pre>Cents cCentsSum = Add(cCents1, cCents2);</pre>
<p>declares an object cCentsSum of type Cents and then initialises it, all on one line. It could have been split into two commands:-</p>
<pre>Cents cCentsSum;
cCentsSum = Add(cCents1, cCents2);</pre>
<p>The Add function returns an object of type Cents so this initialisation is valid. Note that the Add function is not a member of the class cents, it&#8217;s defined outside the class, so is a &#8220;normal&#8221; function. It&#8217;s ok to use functions or expressions as initialisation values, we are not restricted to literals.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://www.learncpp.com/cpp-tutorial/814-anonymous-variables-and-objects/comment-page-1/#comment-89434</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Sun, 01 Aug 2010 21:38:58 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/814-anonymous-variables-and-objects/#comment-89434</guid>
		<description>I totally disagree with the fact that anonymous types cannot be passed by reference to a function.

&lt;pre&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;

using namespace std;

void print(const string &amp; mess) {

     cout &lt;&lt; mess &lt;&lt; endl;

}

int main() {

     print( &quot;Hello world&quot; );

     // or

     print( string( &quot;Hello world&quot; ) + &quot;?&quot; );

     return 0;

}
&lt;!--formatted--&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>I totally disagree with the fact that anonymous types cannot be passed by reference to a function.</p>
<pre>
#include &lt;iostream&gt;
#include &lt;string&gt;

using namespace std;

void print(const string &amp; mess) {

     cout &lt;&lt; mess &lt;&lt; endl;

}

int main() {

     print( &quot;Hello world&quot; );

     // or

     print( string( &quot;Hello world&quot; ) + &quot;?&quot; );

     return 0;

}
<!--formatted--></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Chip</title>
		<link>http://www.learncpp.com/cpp-tutorial/814-anonymous-variables-and-objects/comment-page-1/#comment-86431</link>
		<dc:creator>Chip</dc:creator>
		<pubDate>Mon, 14 Jun 2010 00:16:02 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/814-anonymous-variables-and-objects/#comment-86431</guid>
		<description>Hold your horses. You can write a function of type class? And how this works? : 

Cents cCentsSum = Add(cCents1, cCents2);

Shouldn&#039;t it be like:

Cents cCentsSum(Add(cCents1, cCents2));

I really want to know how things like this work.</description>
		<content:encoded><![CDATA[<p>Hold your horses. You can write a function of type class? And how this works? : </p>
<p>Cents cCentsSum = Add(cCents1, cCents2);</p>
<p>Shouldn&#8217;t it be like:</p>
<p>Cents cCentsSum(Add(cCents1, cCents2));</p>
<p>I really want to know how things like this work.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: CompuChip</title>
		<link>http://www.learncpp.com/cpp-tutorial/814-anonymous-variables-and-objects/comment-page-1/#comment-84401</link>
		<dc:creator>CompuChip</dc:creator>
		<pubDate>Mon, 10 May 2010 10:15:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/814-anonymous-variables-and-objects/#comment-84401</guid>
		<description>In fact, I think you could write the main function without any named variables at all:

&lt;pre&gt;
int main()
{
  std::cout &lt;&lt; &quot;I have &quot; &lt;&lt; Add(Cents(6), Cents(8)).GetCents() &lt;&lt; &quot; cents.&quot; &lt;&lt; std::endl;
  return 0;
}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>In fact, I think you could write the main function without any named variables at all:</p>
<pre>
int main()
{
  std::cout &lt;&lt; &quot;I have &quot; &lt;&lt; Add(Cents(6), Cents(8)).GetCents() &lt;&lt; &quot; cents.&quot; &lt;&lt; std::endl;
  return 0;
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: E-Barto</title>
		<link>http://www.learncpp.com/cpp-tutorial/814-anonymous-variables-and-objects/comment-page-1/#comment-73734</link>
		<dc:creator>E-Barto</dc:creator>
		<pubDate>Sun, 06 Dec 2009 09:12:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/814-anonymous-variables-and-objects/#comment-73734</guid>
		<description>If they weren&#039;t, you couldn&#039;t access them later on in the function:

&lt;pre&gt;
Cents Add(Cents &amp;c1, Cents &amp;c2)
{
    //here
    return Cents(c1.GetCents() + c2.GetCents());
}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>If they weren&#8217;t, you couldn&#8217;t access them later on in the function:</p>
<pre>
Cents Add(Cents &amp;c1, Cents &amp;c2)
{
    //here
    return Cents(c1.GetCents() + c2.GetCents());
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: keshava</title>
		<link>http://www.learncpp.com/cpp-tutorial/814-anonymous-variables-and-objects/comment-page-1/#comment-72772</link>
		<dc:creator>keshava</dc:creator>
		<pubDate>Sun, 22 Nov 2009 07:26:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/814-anonymous-variables-and-objects/#comment-72772</guid>
		<description>nice!!!</description>
		<content:encoded><![CDATA[<p>nice!!!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Josh</title>
		<link>http://www.learncpp.com/cpp-tutorial/814-anonymous-variables-and-objects/comment-page-1/#comment-64971</link>
		<dc:creator>Josh</dc:creator>
		<pubDate>Sat, 25 Jul 2009 02:21:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/814-anonymous-variables-and-objects/#comment-64971</guid>
		<description>&quot;It is not possible to create a pointer or reference to an anonymous variable.&quot;

I beg to differ:

&lt;pre&gt;
int main()
{
    int const &amp; a = (2 + 2);
    std::cout &lt;&lt; a;
}
&lt;!--formatted--&gt;&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>&#8220;It is not possible to create a pointer or reference to an anonymous variable.&#8221;</p>
<p>I beg to differ:</p>
<pre>
int main()
{
    int const &amp; a = (2 + 2);
    std::cout &lt;&lt; a;
}
<!--formatted--></pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: the joker</title>
		<link>http://www.learncpp.com/cpp-tutorial/814-anonymous-variables-and-objects/comment-page-1/#comment-64811</link>
		<dc:creator>the joker</dc:creator>
		<pubDate>Thu, 23 Jul 2009 00:44:12 +0000</pubDate>
		<guid isPermaLink="false">http://www.learncpp.com/cpp-tutorial/814-anonymous-variables-and-objects/#comment-64811</guid>
		<description>&lt;pre&gt; Cents Add(Cents &amp;c1, Cents &amp;c2) &lt;/pre&gt;

i can&#039;t understand why c1 and c2 are passed by reference ??!!</description>
		<content:encoded><![CDATA[<pre> Cents Add(Cents &amp;c1, Cents &amp;c2) </pre>
<p>i can&#8217;t understand why c1 and c2 are passed by reference ??!!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

