<?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>Ajay Kumar Singh &#187; C++</title> <atom:link href="http://www.ajaykumarsingh.com/c/feed" rel="self" type="application/rss+xml" /><link>http://www.ajaykumarsingh.com</link> <description>Blogging [^Singh^]</description> <lastBuildDate>Sat, 04 Feb 2012 23:10:08 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>Singleton Class</title><link>http://www.ajaykumarsingh.com/c/singleton-class.html</link> <comments>http://www.ajaykumarsingh.com/c/singleton-class.html#comments</comments> <pubDate>Mon, 28 Jan 2008 13:53:33 +0000</pubDate> <dc:creator>Ajay</dc:creator> <category><![CDATA[C++]]></category> <category><![CDATA[Singleton Class]]></category> <category><![CDATA[Singleton Design Pattern]]></category> <guid
isPermaLink="false">http://www.ajaykumarsingh.com/?p=11</guid> <description><![CDATA[A singleton class can have only one object. To create singleton class we need to block normal way of creating class by blocking access to the constructor and provide a new function which will return only one instance of the object. This function will also perform checking to see if there is only one instance [...]<h3>Related Posts</h3> No related posts.
]]></description> <content:encoded><![CDATA[<p
class="MsoNormal" style="margin-left: 18pt"><o:p></o:p>A singleton class can have only one object.</p><p
class="MsoNormal" style="margin-left: 18pt">To create singleton class we need to block normal way of creating class by blocking access to the constructor and provide a new function which will return only one instance of the object.</p><p
class="MsoNormal" style="margin-left: 18pt">This function will also perform checking to see if there is only one instance of that object in existence.</p><p
class="MsoNormal" style="margin-left: 18pt">The code flow below describe a way to create a Singleton class</p><p
class="MsoNormal" style="margin-left: 18pt">Class Singleton {</p><p
class="MsoNormal" style="margin-left: 18pt">protected:</p><p
class="MsoNormal" style="margin-left: 18pt">// Stops creating any object via normal route</p><p
class="MsoNormal" style="margin-left: 18pt">Singleton( );</p><p
class="MsoNormal" style="margin-left: 18pt"><o:p></o:p>public:</p><p
class="MsoNormal" style="margin-left: 18pt">Singleton* instance( );</p><p
class="MsoNormal" style="margin-left: 18pt">Singleton(const Singleton &amp;); // Copy constructor</p><p
class="MsoNormal" style="margin-left: 18pt">Singleton &amp; operator= (const Singleton &amp;); // Assignment operator</p><p
class="MsoNormal" style="margin-left: 18pt"><o:p> </o:p></p><p
class="MsoNormal" style="margin-left: 18pt">private:</p><p
class="MsoNormal" style="margin-left: 18pt">static Singleton* instance_ptr;</p><p
class="MsoNormal" style="margin-left: 18pt">};</p><p
class="MsoNormal" style="margin-left: 18pt"><o:p> </o:p></p><p
class="MsoNormal" style="margin-left: 18pt">// Implementation</p><p
class="MsoNormal" style="margin-left: 18pt">Singleton* Singleton::instance_ptr = 0;</p><p
class="MsoNormal" style="margin-left: 18pt">Singleton* Singleton::instance ( )</p><p
class="MsoNormal" style="margin-left: 18pt">{</p><p
class="MsoNormal" style="margin-left: 18pt; text-indent: 18pt">if ( *instance_ptr = = 0)</p><p
class="MsoNormal" style="margin-left: 18pt; text-indent: 18pt">{</p><p
class="MsoNormal" style="margin-left: 54pt; text-indent: 18pt">Instance_ptr = new Singleton;</p><p
class="MsoNormal" style="margin-left: 54pt; text-indent: 18pt">return instance_ptr;</p><p
class="MsoNormal" style="text-indent: 36pt">}</p><p
class="MsoNormal" style="margin-left: 18pt; text-indent: 18pt">else</p><p
class="MsoNormal" style="margin-left: 18pt; text-indent: 18pt"><span>            </span>return 0;</p><p
class="MsoNormal" style="margin-left: 18pt">}</p><p
class="MsoNormal"><o:p> </o:p></p><div
style='clear:both'></div><h3>Related Posts</h3><p>No related posts.</p> ]]></content:encoded> <wfw:commentRss>http://www.ajaykumarsingh.com/c/singleton-class.html/feed</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
