<?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>Daniel Hall&#039;s Website &#187; OOP</title>
	<atom:link href="http://www.danielhall.me/tag/oop/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.danielhall.me</link>
	<description>Because the Internet doesn&#039;t have enough opinions already</description>
	<lastBuildDate>Sun, 23 Oct 2011 23:15:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>C++ Internal Classes</title>
		<link>http://www.danielhall.me/2009/08/c-internal-classes/</link>
		<comments>http://www.danielhall.me/2009/08/c-internal-classes/#comments</comments>
		<pubDate>Wed, 26 Aug 2009 03:16:49 +0000</pubDate>
		<dc:creator>Daniel</dc:creator>
				<category><![CDATA[C/C++]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[OOP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.danielhall.me/?p=266</guid>
		<description><![CDATA[C++, like Java, allows you to have internal classes. You can implement them by simply including the class in the .cpp file and not in the header file. Yep, its that simple. If you try this you will probably realise &#8230;<p class="read-more"><a href="http://www.danielhall.me/2009/08/c-internal-classes/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<p>C++, like Java, allows you to have internal classes. You can implement them by simply including the class in the .cpp file and not in the header file. Yep, its that simple. If you try this you will probably realise that you cant include the class as a data member. This is because when you try to include it in the class the internal class hasn&#8217;t been declared yet. That&#8217;s easy to fix, you can prototype a class by simply adding class ClassName;. Why am I telling you this? Because its an interesting design pattern, and that inner class is called a &#8220;Cheshire Class&#8221;.</p>
<p>The Cheshire Class is a hidden internal class. This doesn&#8217;t immediately sound useful, until you think about how C++ handles private variables. When you add a private variable to a class, even though it is not visible to the calling application, it still affects the structure of the class and hence all the code that links against it will need to be recompiled. The Cheshire class prevents this by keeping all your private variables in a private internal class. See this Example:</p>
<h1>outerclass.h</h1>
<pre lang="c++">#ifndef OUTERCLASS_H_
#define OUTERCLASS_H_

class InnerClass;

class OuterClass {
private:
  InnerClass *internal;
public:
  OuterClass();
  virtual ~OuterClass();
};

#endif /* OUTERCLASS_H_ */</pre>
<h1>outerclass.cpp</h1>
<pre lang="c++">class InnerClass {
public:
  int privateVariable;
};</pre>
<pre>OuterClass::OuterClass() {
  internal.privateVariable = 0;
}

OuterClass::~OuterClass() {

}</pre>
<p>Now instead of adding new private data to the outer class, you add it to the inner class. This way your header stays the exact same, the data structure never changes.</p>
<p>Note that this does take up more space, and takes a little longer to resolve because it has to jump via and extra pointer but sometimes it is worth it for that nicer solution.<br />
<strong>Random Thought:</strong> Which is more valuable, bread or gold? Why? Which is more useful?</p>
]]></content:encoded>
			<wfw:commentRss>http://www.danielhall.me/2009/08/c-internal-classes/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

