<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>GreenwaysRoad Blog &#187; c++</title>
	<atom:link href="http://www.greenwaysroad.com/blogs/vasuBlog/index.php/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.greenwaysroad.com/blogs/vasuBlog</link>
	<description></description>
	<lastBuildDate>Sat, 14 Jan 2012 13:21:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Const in C++</title>
		<link>http://www.greenwaysroad.com/blogs/vasuBlog/index.php/2009/12/12/const-in-c/</link>
		<comments>http://www.greenwaysroad.com/blogs/vasuBlog/index.php/2009/12/12/const-in-c/#comments</comments>
		<pubDate>Sat, 12 Dec 2009 06:20:49 +0000</pubDate>
		<dc:creator>vasu</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[const]]></category>
		<category><![CDATA[overloading]]></category>

		<guid isPermaLink="false">http://www.greenwaysroad.com/blogs/vasuBlog/?p=216</guid>
		<description><![CDATA[Const declaration:

const int* x; //constant data (*x); non-constant pointer
int const *x; //same as above
int * const x; //constant pointer; non-constant data
const int * const x; //const data; const pointer

Const member functions:
Function values can be declared const. Helps specifically in operator overloading.

const Foo operator*(const Foo&#38; lhs, const Foo&#38; rhs);

Foo a,b,c; 

a = b*c; //legal
(a*b) = c; [...]]]></description>
			<content:encoded><![CDATA[<p><B>Const declaration:</B></p>
<pre>
const int* x; //constant data (*x); non-constant pointer
int const *x; //same as above
int * const x; //constant pointer; non-constant data
const int * const x; //const data; const pointer
</pre>
<p><B>Const member functions:</B><br />
Function values can be declared const. Helps specifically in operator overloading.</p>
<pre>
const Foo operator*(const Foo&amp; lhs, const Foo&amp; rhs);

Foo a,b,c; 

a = b*c; //legal
(a*b) = c; //This will work if return value is not const
</pre>
<p>It is not useful to assign any value to the product of two variables but it can happen accidentally. Example, using = instead of ==</p>
<pre>
if(a*b = c) {
}
</pre>
<p>Const member functions in a class cannot modify the contents of the class. They make it possible to work with const objects. Member functions differing only in their &#8220;constness&#8221; can be overloaded.</p>
<pre>
class Foo {
public:
&nbsp;&nbsp;&nbsp; const int&amp; value() const;
&nbsp;&nbsp;&nbsp;    int&amp; value();

private:
&nbsp;&nbsp;&nbsp; int x;
};

main() {
&nbsp;&nbsp;&nbsp; Foo x(1);
&nbsp;&nbsp;&nbsp; cout&lt;&lt;x.value(); //Non-const function is called

&nbsp;&nbsp;&nbsp; const Foo y(2);
&nbsp;&nbsp;&nbsp; cout&lt;&lt;y.value(); //const function is called
}
</pre>
<p><B>Bitwise Constness:</B><br />
C++ gives only bitwise constness. This means const member functions cannot modify pointers in a class. But they can modify the data the pointer points to.</p>
<p><B>Mutable:</B><br />
Mutable type modifier allows variables to be modified even by const functions.</p>
<pre>
Class Foo {
public:
&nbsp;&nbsp;&nbsp; void test() const; //can modify x but nor y
private:
&nbsp;&nbsp;&nbsp; mutable int x;
&nbsp;&nbsp;&nbsp; int y;
};
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.greenwaysroad.com/blogs/vasuBlog/index.php/2009/12/12/const-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

