<?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>Visual C++ Tips &#187; functors</title>
	<atom:link href="http://weseetips.com/tag/functors/feed/" rel="self" type="application/rss+xml" />
	<link>http://weseetips.com</link>
	<description>Gold mine of Visual C++ tricks!</description>
	<lastBuildDate>Thu, 11 Mar 2010 09:09:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Function Objects or simply Functors.</title>
		<link>http://weseetips.com/2008/05/20/function-objects-or-simply-functors/</link>
		<comments>http://weseetips.com/2008/05/20/function-objects-or-simply-functors/#comments</comments>
		<pubDate>Tue, 20 May 2008 18:58:38 +0000</pubDate>
		<dc:creator>Jijo Raj</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Codeproject]]></category>
		<category><![CDATA[function object]]></category>
		<category><![CDATA[function objects]]></category>
		<category><![CDATA[functor]]></category>
		<category><![CDATA[functors]]></category>

		<guid isPermaLink="false">http://weseetips.wordpress.com/?p=126</guid>
		<description><![CDATA[A function object is an ordinary class object with operator () overloaded. The specialty of functor is that the object can also act as function calls. But the power of a functor is &#8211; it can hold state. See a sample code snippet for implementing a functor which compares two strings &#8211; with and with [...]]]></description>
			<content:encoded><![CDATA[<p><img class="alignnone size-medium wp-image-11" src="http://weseetips.wordpress.com/files/2008/03/icon_description.jpg?w=166" alt="" width="166" height="32" /><br />
A <span style="color: #0000ff;">function object</span> is an ordinary class object <span style="color: #0000ff;">with operator () overloaded</span>. The specialty of functor is that the <span style="color: #0000ff;">object can also act as function calls</span>. But the power of a functor is &#8211; <span style="color: #0000ff;">it can hold state</span>.</p>
<p><img class="alignnone size-medium wp-image-12" src="http://weseetips.wordpress.com/files/2008/03/icon_howcanidoit.jpg?w=220" alt="" width="220" height="32" /><br />
See a sample code snippet for implementing a functor which compares two strings &#8211; with and with out case sensitive.</p>
<pre>class Compare
{
public:
    // Constructor.
    Compare()
        : m_CaseSensitive(true)
    {}

    // overloaded () operator.
    BOOL operator()( CString&amp; csString1, CString&amp; csString2)
    {
        if( m_CaseSensitive )
        {
            // Case sensitive comparison.
            return csString1 == csString2;
        }
        else
        {
            // Non Case sensitive comparison.
            return (csString1.CompareNoCase( csString2 ) == 0) ? TRUE : FALSE;
        }
    }

    // Set the comparison.
    void SetComparisonCaseSensitive( bool CaseSensitive )
    { m_CaseSensitive = CaseSensitive; }

private:
    bool m_CaseSensitive;
};

void CompareStrings()
{
    CString csString1 = _T("Hello");
    CString csString2 = _T("hello");

    // The functor.
    Compare CompareStrings;

    // Demo of case sensitive comparison.
    BOOL bCaseSensitiveComparison = CompareStrings( csString1,
                                                    csString2);

    // non Case sensitive comparison.
    CompareStrings.SetComparisonCaseSensitive( false );
    // Demo of case sensitive comparison.
    BOOL bNonCaseSensitiveComparison = CompareStrings( csString1,
                                                       csString2);
}</pre>
<p>In the code snippet, a string comparison is implemented by using a functor. Ideally if you want the string comparison to be able to handle both case sensitive and non-case sensitive comparisons, you&#8217;ve to use some boolean values globally or should pass it as function parameters. Or else you&#8217;ve to implement two different functions to implement both of them.</p>
<p>Here comes the real power of a functor. Since Its a class, can hold parameters. So that you can avoid global variables, may be some function parameters etc. Try functors&#8230; its fun!</p>
<p><img class="alignnone size-medium wp-image-18" src="http://weseetips.wordpress.com/files/2008/03/icon_note.jpg?w=94" alt="" width="94" height="32" /><br />
Don’t think that functors is a new feature introduced by C++. Functors exists long before the days of C++ <img src='http://weseetips.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  One of the early languages that implemented functors was SmallTalk.</p>
<p><img class="alignnone size-medium wp-image-53" src="http://weseetips.wordpress.com/files/2008/03/intermediateseries.jpg?w=248" alt="" width="248" height="32" /><br />
Targeted Audience &#8211; Intermediate.</p>
]]></content:encoded>
			<wfw:commentRss>http://weseetips.com/2008/05/20/function-objects-or-simply-functors/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

