<?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>iOS Developer Tips Blog &#187; Steve</title>
	<atom:link href="http://iPhoneDeveloperTips.com/author/steve/feed" rel="self" type="application/rss+xml" />
	<link>http://iPhoneDeveloperTips.com</link>
	<description>iOS Developer Tips, Tricks and Tutorials.</description>
	<lastBuildDate>Mon, 30 Jan 2012 07:09:56 +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>Of BOOL and YES</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/of-bool-and-yes.html</link>
		<comments>http://iPhoneDeveloperTips.com/objective-c/of-bool-and-yes.html#comments</comments>
		<pubDate>Wed, 31 Dec 2008 19:28:56 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[Objective-C]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=1422</guid>
		<description><![CDATA[It may not be immediately clear that the Objective C BOOL &#34;type&#34; is not actually a boolean type at all. This is a legacy from the original C language, which does not have an intrinsic boolean type (the iPhone GCC C compiler supports the ISO C99 standard which does define a bool type). To clarify, [...]]]></description>
			<content:encoded><![CDATA[<p>It may not be immediately clear that the Objective C <code>BOOL</code> &quot;type&quot; is not actually a boolean type at all. This is a legacy from the original C language, which does not have an intrinsic boolean type (the iPhone GCC C compiler supports the ISO C99 standard which does define a <code>bool</code> type). To clarify, in the system header file <code>objc.h</code> you can see the following:<br />
<span id="more-1422"></span></p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">typedef</span> <span style="color: #a61390;">signed</span> <span style="color: #a61390;">char</span>        <span style="color: #a61390;">BOOL</span>;</pre></div></div>

<p>From this it is clear that <code>BOOL</code> is actually a signed char type. This has some implications when it comes to using the <code>YES</code> and <code>NO</code> values that are typically assigned to variables of &quot;type&quot; <code>BOOL</code>. If we look a little further down <code>objc.h</code> you will see the following:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #6e371a;">#define YES             (BOOL)1</span>
<span style="color: #6e371a;">#define NO              (BOOL)0</span></pre></td></tr></table></div>

<p>So what is the problem with <code>BOOL</code> and <code>YES</code>? In a nutshell, the problem is that variables of type <code>BOOL</code> can contain values other than <code>YES</code> and <code>NO</code>. Comparing for <code>NO</code> is not an issue since <code>NO == 0 == NO</code>, but comparing for <code>YES</code> can be tricky, especially if you are relying on 3rd party code to play by the rules. Consider this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #a61390;">BOOL</span> b <span style="color: #002200;">=</span> <span style="color: #2400d9;">37</span>;
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>b<span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;b is YES!<span style="color: #2400d9;">\n</span>&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span>
<span style="color: #a61390;">if</span> <span style="color: #002200;">&#40;</span>b <span style="color: #002200;">!=</span> <span style="color: #a61390;">YES</span><span style="color: #002200;">&#41;</span> <span style="color: #002200;">&#123;</span>
    <span style="color: #a61390;">printf</span><span style="color: #002200;">&#40;</span><span style="color: #bf1d1a;">&quot;b is not YES!<span style="color: #2400d9;">\n</span>&quot;</span><span style="color: #002200;">&#41;</span>;
<span style="color: #002200;">&#125;</span></pre></td></tr></table></div>

<p>The output from this would be as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;">b is <span style="color: #a61390;">YES</span><span style="color: #002200;">!</span>
b is not <span style="color: #a61390;">YES</span><span style="color: #002200;">!</span></pre></td></tr></table></div>

<p>The problem arises because direct comparison with <code>YES</code> will fail when the value of a <code>BOOL</code> type is a non-zero value other than 1.</p>
<p>Note that ObjC (assuming the default C99 setting is enabled) also supports the <code>bool</code> data type, which is an intrinsic boolean type and can only be set to <code>true</code> or <code>false</code>. A safe way to convert between <code>BOOL</code> and <code>bool</code> is as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="objc" style="font-family:monospace;"><span style="color: #11740a; font-style: italic;">// from BOOL to bool</span>
bool b <span style="color: #002200;">=</span> myBOOL ? <span style="color: #a61390;">true</span> <span style="color: #002200;">:</span> <span style="color: #a61390;">false</span>;
<span style="color: #11740a; font-style: italic;">// and back</span>
myBOOL <span style="color: #002200;">=</span> b ? <span style="color: #a61390;">YES</span> <span style="color: #002200;">:</span> <span style="color: #a61390;">NO</span>;</pre></td></tr></table></div>

<p>The moral of the story is to avoid direct comparisons between <code>BOOL</code> types and the <code>YES</code> constant.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/objective-c/of-bool-and-yes.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>C++ on iPhone: Part 3, Run Time Type Identification</title>
		<link>http://iPhoneDeveloperTips.com/cpp/c-on-iphone-part-3-run-time-type-identification.html</link>
		<comments>http://iPhoneDeveloperTips.com/cpp/c-on-iphone-part-3-run-time-type-identification.html#comments</comments>
		<pubDate>Thu, 06 Nov 2008 04:59:51 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=980</guid>
		<description><![CDATA[In this third article on C++ programming on the iPhone platform, I will be covering RTTI, or Run Time Type Identification. This C++ language feature is often unsupported on mobile platforms due to the usual &#34;code bloat&#34; reasons. So, let&#8217;s take a look at the iPhone and see what it has to offer. There are [...]]]></description>
			<content:encoded><![CDATA[<p>In this third article on C++ programming on the iPhone platform, I will be covering RTTI, or Run Time Type Identification. This C++ language feature is often unsupported on mobile platforms due to the usual &quot;code bloat&quot; reasons. So, let&#8217;s take a look at the iPhone and see what it has to offer.<br />
<span id="more-980"></span></p>
<p>There are two main elements of RTTI in C++, namely <strong>type identification</strong>, and <strong>type casting</strong>.</p>
<p><strong>1. Type identification.</strong> The C++ keyword <code><strong>typeid</strong></code> is an operator used to obtain information about the run time type of an object. It is used as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// typeid only works on polymorphic classes,</span>
<span style="color: #666666;">// that is, classes with at least one virtual method</span>
&nbsp;
<span style="color: #0000ff;">class</span> PolymorphicClass
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
<span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> virtualMethod<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span> polymorphicObject<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">const</span> std<span style="color: #008080;">::</span><span style="color: #007788;">type_info</span> <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>info <span style="color: #000080;">=</span> <span style="color: #0000ff;">typeid</span> <span style="color: #008000;">&#40;</span>polymorphicObject<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>In this case, std::type_info is a type that will receive the RTTI information about our polymorphicObject &#8211; we&#8217;ll actually get a reference to a std::type_info back from the typeid operator. The std::type_info class can be used to obtain the actual name of the type of our polymorphicObject, as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> name <span style="color: #000080;">=</span> info.<span style="color: #007788;">name</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>Which will yield a string representation of the class name, but not necessarily the actual class name. I believe the string returned is &#8220;implementation dependent&#8221; and as such may vary from compiler to compiler. We&#8217;ll see what we get on the iPhone shortly.<br />
<strong><br />
2. Dynamic type casting.</strong> The C++ operator <code><strong>dynamic_cast</strong></code> allows casting &quot;down&quot; a polymorphic inheritance tree (ie, casting from a base class to a derived class). It operates on pointer (and reference) types and the syntax is as follows:</p>
<p>pointer to object of target type = dynamic_cast(pointer expression);<br />
reference to object of target type = dynamic_cast(reference expression);</p>
<p>To see this in action, consider the following class definitions:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;">Class InterfaceClass
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    <span style="color: #0000ff;">virtual</span> GeneralMethod<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
Class SpecializedClass <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> InterfaceClass
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> GenerealMethod<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> SpecializedMethod<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Then, let&#8217;s say we have a base class pointer that actually points to a specialized class.</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">InterfaceClass<span style="color: #002200;">*</span> interface <span style="color: #002200;">=</span> new SpecializedClass<span style="color: #002200;">&#40;</span><span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>There is no way to call the method SpecializedMethod() from this base class pointer directly; however, if we know the pointer is to a SpecializedClass, we can use the <code>dynamic_cast</code> operator thus:</p>

<div class="wp_syntax"><div class="code"><pre class="objc" style="font-family:monospace;">SpecializedClass<span style="color: #002200;">*</span> specialized <span style="color: #002200;">=</span> dynamic_cast<span style="color: #002200;">&amp;</span>lt;SpecializedClass<span style="color: #002200;">*&amp;</span>gt;<span style="color: #002200;">&#40;</span>interface<span style="color: #002200;">&#41;</span>;</pre></div></div>

<p>There is much more to this topic than I have space to go into here, but we&#8217;ve at least covered some of the basics. The question remaining is, &#8220;will it work in the iPhone&#8221;? In fact, this works perfectly well, and I&#8217;ve wrapped the above into an example you can try. Using the same project as the previous articles, I made myself new files rtti.cpp and rtti.h and added them to the project. In this case, I am not going to make a class called &#8220;rtti&#8221; but these files will hold our RTTI test code.</p>
<p>Our header file rtti.h looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// rtti.h</span>
<span style="color: #339900;">#ifdef __cplusplus</span>
<span style="color: #0000ff;">extern</span> <span style="color: #FF0000;">&quot;C&quot;</span> <span style="color: #008000;">&#123;</span>
<span style="color: #339900;">#endif</span>
	<span style="color: #0000ff;">void</span> rttiTest<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #339900;">#ifdef __cplusplus</span>
<span style="color: #008000;">&#125;</span>
<span style="color: #339900;">#endif</span></pre></td></tr></table></div>

<p>And rtti.cpp looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// rtti.cpp</span>
&nbsp;
<span style="color: #339900;">#include &quot;rtti.h&quot;</span>
<span style="color: #339900;">#include &amp;lt;typeinfo&amp;gt;</span>
<span style="color: #339900;">#include &amp;lt;iostream&amp;gt;</span>
&nbsp;
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// test typeid</span>
<span style="color: #0000ff;">class</span> PolymorphicClass
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	<span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> virtualMethod<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span> polymorphicObject<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// test dynamic_cast</span>
<span style="color: #0000ff;">class</span> InterfaceClass
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> GeneralMethod<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> SpecializedClass <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> InterfaceClass
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> GenerealMethod<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
	<span style="color: #008000;">&#125;</span>
    <span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">void</span> SpecializedMethod<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> <span style="color: #FF0000;">&quot;SpecializedMethod()&quot;</span> <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">void</span> rttiTest<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">const</span> type_info <span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span>info <span style="color: #000080;">=</span> <span style="color: #0000ff;">typeid</span> <span style="color: #008000;">&#40;</span>polymorphicObject<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> name <span style="color: #000080;">=</span> info.<span style="color: #007788;">name</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> <span style="color: #FF0000;">&quot;typeid name of PolyMorphicClass is &quot;</span> <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> name <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	InterfaceClass<span style="color: #000040;">*</span> interface <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> SpecializedClass<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// can't do this:</span>
	<span style="color: #666666;">// interface-&amp;gt;SpecializedMethod();</span>
&nbsp;
	<span style="color: #666666;">// but use dynamic_cast to down cast the pointer</span>
	SpecializedClass<span style="color: #000040;">*</span> specialized <span style="color: #000080;">=</span> <span style="color: #0000ff;">dynamic_cast</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span>SpecializedClass<span style="color: #000040;">*</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span><span style="color: #008000;">&#40;</span>interface<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>	
&nbsp;
	<span style="color: #666666;">// and then this works fine:</span>
	specialized<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>SpecializedMethod<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #666666;">// and for good measure print the names of pointee objects</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> <span style="color: #FF0000;">&quot;Run time type name of interface is &quot;</span> <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> <span style="color: #0000ff;">typeid</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>interface<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">name</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cout</span> <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> <span style="color: #FF0000;">&quot;Run time type name of specialized is &quot;</span> <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> <span style="color: #0000ff;">typeid</span><span style="color: #008000;">&#40;</span><span style="color: #000040;">*</span>specialized<span style="color: #008000;">&#41;</span>.<span style="color: #007788;">name</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> endl<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Then I included rtti.h from main.m and added a called to the rttiTest() method at the top of main():</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;">	rttiTest<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></div></div>

<p>The console output when this is run on the iPhone is as follows:</p>
<pre>
typeid name of PolyMorphicClass is 16PolymorphicClass
SpecializedMethod()
Run time type name of interface is 16SpecializedClass
Run time type name of specialized is 16SpecializedClass
</pre>
<p>Notice the name given is &#8220;16PolymorphicClass&#8221;, which is pretty typical of the type of name you&#8217;ll see wen using typeid. It is useful for comparison purposes, if nothing else. I believe this is an artifact of the &#8220;mangling&#8221; that occurs to all C++ symbols during compilation. I&#8217;ve also added output to show the run time type names of the object pointed @ by <code>interface</code> and <code>specialized</code>, for good measure (they are the same, as you would expect).</p>
<p>To recap, we have shown through an extremely simple example that basic RTTI operations do in fact work on the iPhone. We saw that <code>typeid</code> gives us run time type info about an object, and that <code>dynamic_cast</code> correctly casts down a polymorphic class hierarchy.</p>
<p>In upcoming articles I will be moving on to examples of interaction between Objective C, Objective C++ and straight C++ code.</p>
<p><em>I would add as a footnote that dynamic_cast is potentially a performance hog and can often be avoided in favor of the alternate static_cast operator (which is resolved at compile time). Still, as a C++ language feature it is good to know that it is available.</em> </p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/cpp/c-on-iphone-part-3-run-time-type-identification.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>C++ on iPhone: Part 2, Exceptions</title>
		<link>http://iPhoneDeveloperTips.com/objective-c/c-on-iphone-part-2-exceptions.html</link>
		<comments>http://iPhoneDeveloperTips.com/objective-c/c-on-iphone-part-2-exceptions.html#comments</comments>
		<pubDate>Mon, 27 Oct 2008 05:54:01 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[C++]]></category>
		<category><![CDATA[Objective-C]]></category>
		<category><![CDATA[C]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=881</guid>
		<description><![CDATA[In part 2 of this C++ on iPhone series I&#8217;ll be exploring C++ exception handling support, and as a bonus I&#8217;ll touch on use of standard C++ lib console output stream, as well as showing a way to call C++ code from Objective C. As a reminder, exception handling is normally one of the weak [...]]]></description>
			<content:encoded><![CDATA[<p>In part 2 of this C++ on iPhone series I&#8217;ll be exploring C++ exception handling support, and as a bonus I&#8217;ll touch on use of standard C++ lib console output stream, as well as showing a way to call C++ code from Objective C. As a reminder, exception handling is normally one of the weak spots in &quot;mobile device&quot; C++ support, so I wanted to find out how well it is supported on iPhone.</p>
<p>Jumping right into some code, using the same Xcode project as in part 1, I added a new C++ class <code>Exceptions</code> (adding <code>Exceptions.cpp</code> and <code>Exceptions.h</code> to the project).<br />
<span id="more-881"></span></p>
<p><code>Exceptions.h</code> looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;exception&gt;</span>
<span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> Exceptions
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
	<span style="color: #0000ff;">static</span> <span style="color: #0000ff;">void</span> test<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> whichException<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>This is a very simple class with just one static member function used to exercise exception handling support. The only noteworthy thing here is that some of the C++ standard library features are used, namely the #includes of <code>&lt;iostream&gt;</code> (used here to output logging information to the console) and <code>&lt;exception&gt;</code> which is a base class intended to be sub-classed by applications. Additionally, we need to specify &quot;<code>using namespace std</code> &quot; to avoid specifying the &quot;<code>std::</code> &quot; prefix on all standard C++ library classes.</p>
<p><code>Exceptions.cpp</code> looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#import &amp;quot;Exceptions.h&amp;quot;</span>
<span style="color: #339900;">#import &amp;quot;ExceptionsBridge.h&amp;quot;</span>
&nbsp;
<span style="color: #0000ff;">void</span> exceptionTest<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	Exceptions<span style="color: #008080;">::</span><span style="color: #007788;">test</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	Exceptions<span style="color: #008080;">::</span><span style="color: #007788;">test</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	Exceptions<span style="color: #008080;">::</span><span style="color: #007788;">test</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	Exceptions<span style="color: #008080;">::</span><span style="color: #007788;">test</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">class</span> MyException1 <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> exception
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> what<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">throw</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #000040;">&amp;</span>quot<span style="color: #008080;">;</span>MyException1 was thrown<span style="color: #000040;">&amp;</span>quot<span style="color: #008080;">;;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span> myException1<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> MyException2 <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> exception
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> what<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">throw</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #000040;">&amp;</span>quot<span style="color: #008080;">;</span>MyException2 was thrown<span style="color: #000040;">&amp;</span>quot<span style="color: #008080;">;;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span> myException2<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> MyException3 <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> exception
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> what<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">throw</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #000040;">&amp;</span>quot<span style="color: #008080;">;</span>MyException3 was thrown<span style="color: #000040;">&amp;</span>quot<span style="color: #008080;">;;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span> myException3<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">class</span> BadException <span style="color: #008080;">:</span> <span style="color: #0000ff;">public</span> exception
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">virtual</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">char</span><span style="color: #000040;">*</span> what<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">throw</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">return</span> <span style="color: #000040;">&amp;</span>quot<span style="color: #008080;">;</span>BadException was thrown<span style="color: #000040;">&amp;</span>quot<span style="color: #008080;">;;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span> badException<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">void</span> Exceptions<span style="color: #008080;">::</span><span style="color: #007788;">test</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> whichException<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">try</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">switch</span><span style="color: #008000;">&#40;</span>whichException<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
		<span style="color: #0000ff;">case</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">:</span>
			<span style="color: #0000ff;">throw</span> myException1<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">case</span> <span style="color: #0000dd;">2</span><span style="color: #008080;">:</span>
			<span style="color: #0000ff;">throw</span> myException2<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">case</span> <span style="color: #0000dd;">3</span><span style="color: #008080;">:</span>
			<span style="color: #0000ff;">throw</span> myException3<span style="color: #008080;">;</span>
		<span style="color: #0000ff;">default</span><span style="color: #008080;">:</span>
			<span style="color: #0000ff;">throw</span> badException<span style="color: #008080;">;</span>
		<span style="color: #008000;">&#125;</span>
	<span style="color: #008000;">&#125;</span>
	<span style="color: #0000ff;">catch</span> <span style="color: #008000;">&#40;</span>exception<span style="color: #000040;">&amp;</span>amp<span style="color: #008080;">;</span> e<span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> e.<span style="color: #007788;">what</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span><span style="color: #000040;">&amp;</span>lt<span style="color: #008080;">;</span> endl<span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>There are a number of things happening here. First, you&#8217;ll notice the <code>#include</code> of <code>ExceptionsBridge.h</code> , more on this later. We also derive 4 custom classes from the standard library &quot;<code>exception</code> &quot; base class, namely <code>MyException1-3</code> and <code>BadException</code> . These are declared and instantiated in place using the declarators <code>myException1-3</code> and <code>badException</code> respectively.</p>
<p>We also implement the static <code>Exceptions::test()</code> method, which simply takes an int parameter and uses it to decide which exception type to throw. The throws are wrapped in a <code>try{}</code> block, with a corresponding <code>catch{exception&amp;}</code> statement, which catches a reference to the base exception class, so that all thrown exceptions are caught internally. The catch block also writes log output to <code>cout</code> which is a standard C++ library console output stream.</p>
<p>We also implement the C-style method <code>exceptionTest()</code> at global scope, the definition looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">void</span> exceptionTest<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	Exceptions<span style="color: #008080;">::</span><span style="color: #007788;">test</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	Exceptions<span style="color: #008080;">::</span><span style="color: #007788;">test</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	Exceptions<span style="color: #008080;">::</span><span style="color: #007788;">test</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	Exceptions<span style="color: #008080;">::</span><span style="color: #007788;">test</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">4</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>The <code>exceptionTest()</code> method simply calls <code>Exception::test()</code> with various parameters and is not part of a class, so is callable from C language. In order to do this we use the <code>ExceptionsBridge.h</code> file which is included both from the C++ side (in <code>Exceptions.cpp</code> ) and from the Objective C side (in <code>main.m</code> , as we&#8217;ll see shortly).</p>
<p>The <code>ExceptionsBridge.h</code> file looks like this:</p>
<pre>#ifdef __cplusplus
extern &quot;C&quot; {
#endif

void exceptionTest();

#ifdef __cplusplus
}
#endif</pre>
<p>This is the secret sauce that provides a way for Objective-C code to communicate with C++ code. In order for the <code>exceptionTest()</code> method to be visible from Objective C code, we need to do two things: i) declare the method as <code>extern &quot;C&quot;</code> from the C++ side (this will provide an unmangled symbol export to make the method visible to Objective C), and ii) forward declare the method from the Objective C side (so that the compiler knows it is external and will be resolved at link time). This header accomplishes both by using the preprocessor to test the compiler defined symbol <code>__cplusplus</code> , which is only defined when compiling a C++ file. A bridging header like this could be used to bridge all touch points between Objective C and C++ code (though there are other ways to do this using Objective C++ features, which I plan to cover in a future article).</p>
<p>Then, to complete the picture, we add the following to <code>main.m</code></p>
<p><code>#import &quot;ExceptionsBridge.h&quot;    // this goes immediately above the main() method</code></p>
<p>And,</p>
<p><code> exceptionTest();    // make this the first thing the main() method does</code></p>
<p><em>Note, if you are following along at home, remove any code added to the <code>main()</code> method in the previous article.</em></p>
<p>OK, so that is all the code. To recap, here&#8217;s what we did:</p>
<p>C++ side:</p>
<ul>
<li>defined some custom C++ exception types derived from the standard lib exception base class</li>
<li>created a simple C++ class with a static method that throws and catches the custom exception types</li>
<li>logged output using the standard iostream classes</li>
<li>wrapped all this in a C function exported to be visible to the Objective C side</li>
</ul>
<p>Objective C side:</p>
<ul>
<li>imported the C wrapper function</li>
<li>invoked the C wrapper function</li>
</ul>
<p>When this is built and run, we see the following on the console:</p>
<pre>MyException1 was thrown
MyException2 was thrown
MyException3 was thrown
BadException was thrown</pre>
<p>This is completely as expected, each of our custom exception types was thrown and caught. This is good news, it means that, at least in simple tests such as these, C++ support on iPhone is shaping up to be pretty nice: in part 1 we saw that global constructors are supported and here in part 2 we see that exception handling is supported. Coming up in part 3 is RTTI support.</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/objective-c/c-on-iphone-part-2-exceptions.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>C++ on iPhone: Part 1a</title>
		<link>http://iPhoneDeveloperTips.com/cpp/c-on-iphone-part-1a.html</link>
		<comments>http://iPhoneDeveloperTips.com/cpp/c-on-iphone-part-1a.html#comments</comments>
		<pubDate>Sun, 26 Oct 2008 18:10:24 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=869</guid>
		<description><![CDATA[Upon completing a followup article to part 1 in this series on C++ for the iPhone, I realized that the article was not really very iPhone specific, and was more of a generic C++ article. That being the case, I&#8217;ve rewritten the article and posted on my site here. The article pertains to some interesting [...]]]></description>
			<content:encoded><![CDATA[<p>Upon completing a followup article to <a href="http://iphonedevelopertips.com/c/c-on-iphone-part-1.html">part 1</a> in this series on C++ for the iPhone, I realized that the article was not really very iPhone specific, and was more of a generic C++ article.</p>
<p>That being the case, I&#8217;ve rewritten the article and posted on my site <a href="http://stevewetherill.com/2008/10/25/c-static-global-initialization/">here</a>. The article pertains to some interesting static initialization ordering issues wrt C++ in general.</p>
<p>That being said, I did originate the article with examples running in Xcode, and I can report that the behavior I observed running the same code on the iPhone device was identical to that seen running on MSVC. This is really just a long-winded way of saying that once again, the iPhone demonstrates the &#8220;expected&#8221; behavior when running C++ code (even if the expected behavior is not what you might initially expect!).</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/cpp/c-on-iphone-part-1a.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>C++ on iPhone: Part 1</title>
		<link>http://iPhoneDeveloperTips.com/cpp/c-on-iphone-part-1.html</link>
		<comments>http://iPhoneDeveloperTips.com/cpp/c-on-iphone-part-1.html#comments</comments>
		<pubDate>Tue, 21 Oct 2008 06:44:56 +0000</pubDate>
		<dc:creator>Steve</dc:creator>
				<category><![CDATA[C++]]></category>

		<guid isPermaLink="false">http://iPhoneDeveloperTips.com/?p=825</guid>
		<description><![CDATA[Much is made of the Objective C programming language when discussing iPhone development. It&#8217;s the language of choice, many of the iPhone frameworks are written in Obj-C, etc, etc. Coming from a background of mobile development on other platforms, I was curious about how well C++ is supported on the iPhone. BREW, Symbian and Windows [...]]]></description>
			<content:encoded><![CDATA[<p>Much is made of the Objective C programming language when discussing iPhone development. It&#8217;s the language of choice, many of the iPhone frameworks are written in Obj-C, etc, etc.</p>
<p>Coming from a background of mobile development on other platforms, I was curious about how well C++ is supported on the iPhone. BREW, Symbian and Windows Mobile all have C++ support in one shape or another, and certainly there is a ton of C++ code out there, so my thought was this: will legacy C++ code from other platforms run on iPhone?<br />
<span id="more-825"></span></p>
<p>The first thing to realize is that under the Xcode hood, Apple is using GCC (the GNU Compiler Collection) compiler for the Obj-C compilation. This is important because the GCC Obj-C compiler is in a manner of speaking also the GCC C++ compiler, with a few different command line switches. So, it seems promising &#8211; perhaps C++ support will work. But, what about run time library support? The other major platforms using the GCC tools (BREW, Symbian) have various horrible (and arguably pointless given today&#8217;s hardware) restrictions on what you can do, with various C++ &#8220;language features&#8221; simply being dropped (with various justifications). Here&#8217;s a short list of the things you typically don&#8217;t get with other mobile C++ tool chains:</p>
<p><strong>1. Global static objects.</strong> Static C++ objects are regarded as a no-no for various reasons on other platforms. Both BREW and Symbian want you to reference all global application data through a pointer to allocated application heap. You&#8217;ll typically see some sort of &#8220;Applet&#8221; structure, in which the developer can embed pointers to objects that must be allocated by the application at init time. Then, there is a process of passing the pointer to ths &#8220;applet&#8221; struct around to any object that needs it, or else there will be a method provided in the platform API which will return the address of the applet struct. There&#8217;s good and bad with C++ objects defined at global scope, ordering issues, and so on; however, they can be useful, for example with smart pointers, which have the advantage of cleaning up after themselves when defined @ global scope (whereas a regular pointer would not have its destructor called as we&#8217;ll see later). Will iPhone support this?</p>
<p><strong>2. Exception handling.</strong> Citing code bloat, this feature is often not supported. You can make it work on BREW w/GCC if you are prepared to do runtime lib hacking, and Symbian (last I checked) has their own non-standard variant, but the standard C++ exception handling is typically missing. Does the iPhone support this? We&#8217;re not so worried about the code size on the iPhone &#8211; the 10MB OTA AppStore limit gives plenty of room for manuever.</p>
<p><strong>3. RTTI &#8211; run time type identification.</strong> This is typically not supported, the reasons given being code bloat and lack of exception support (what to do if a cast fails?). Again, does iPhone support this?</p>
<p>It is also important to understand how C++ code interacts with other code written in C and Obj-C. After all, having C++ support is all well and good, but it has to be practically useful, calling back and forth, etc. That said, I am going leave that aspect for a future article, and focus here on pure C++ global static objects &#8211; does the iPhone support them at all? The short answer is that, yes, it does.</p>
<p><em>Note: typically you would not really have a bunch of &#8220;loose&#8221; global static objects like this &#8211; you&#8217;d probably want to make them static class members; however, for the purposes of this article I&#8217;m simply going to define some objects at file scope, for clarity.</em></p>
<p>Here&#8217;s what I did. I started with the generic iPhone &#8220;Window Based Application&#8221; Xcode template project. To this, I then started adding C++ classes.</p>
<p>1. CTRL-click on Other Sources, and select Add -&gt; New File<br />
2. In the Mac OSX category, click on C and C++ and then select the C++ File template.<br />
3. I gave my class the name GlobalStatic.cpp and selected &#8216;also create GlobalStatic.h&#8217;</p>
<p>This gives us empty C++ header and implementation files, the contents of which I will detail below.</p>
<p>Next, I followed the same three steps and made myself another C++ class called Tests (Tests.cpp/h).</p>
<p>OK, so the GlobalStatic class looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// GlobalStatic.h</span>
&nbsp;
<span style="color: #0000ff;">class</span> GlobalStatic
<span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
    GlobalStatic<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    GlobalStatic<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> integerValue<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    ~GlobalStatic<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
    <span style="color: #0000ff;">int</span> integerValue<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>This is a simple class with 2 constructor overloads, a destructor and an int instance var.</p>
<p>The implementation looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// GlobalStatic.cpp</span>
&nbsp;
<span style="color: #339900;">#include &amp;lt;stdio.h&amp;gt;</span>
<span style="color: #339900;">#include &quot;GlobalStatic.h&quot;</span>
&nbsp;
<span style="color: #666666;">// default constructor</span>
GlobalStatic<span style="color: #008080;">::</span><span style="color: #007788;">GlobalStatic</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008080;">:</span> integerValue<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">0</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;GlobalStatic()<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
GlobalStatic<span style="color: #008080;">::</span><span style="color: #007788;">GlobalStatic</span><span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> integerValue<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;GlobalStatic(%d)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, integerValue<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    this<span style="color: #000040;">-</span><span style="color: #000040;">&amp;</span>gt<span style="color: #008080;">;</span>integerValue<span style="color: #000080;">=</span>integerValue<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #666666;">// destructor</span>
GlobalStatic<span style="color: #008080;">::</span>~GlobalStatic<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000dd;">printf</span><span style="color: #008000;">&#40;</span><span style="color: #FF0000;">&quot;~GlobalStatic(%d)<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, integerValue<span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>I include the C runtime stdio.h file so I can call printf(), which outputs text to the debug console (in this case). Other than that, this is a very basic class who&#8217;s main raison d&#8217;etre is to call printf() to let us know when the ctor and dtor are called. OK, so far so good.</p>
<p><em>Note: the difference between #import and #include is basically that #import has &#8220;include guards&#8221; built in, whereas #include does not. Simply put, using #import will ensure that a header file is not included twice. I&#8217;ve used #import for Objective-C headers and #include for the C++ headers, but I&#8217;ve omitted include guards in the actual headers to keep things simple.</em></p>
<p>Now, for the purposes of this exercise the file Test.h remains empty. In Tests.cpp, I define a few global static objects, as follows:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #666666;">// Tests.cpp</span>
&nbsp;
<span style="color: #339900;">#include &quot;Tests.h&quot;</span>
<span style="color: #339900;">#include &quot;GlobalStatic.h&quot;;</span>
&nbsp;
<span style="color: #666666;">// define a global static object with default constructor</span>
GlobalStatic gs1<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// define spme global static objects with inieger constructor</span>
GlobalStatic gs2<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
GlobalStatic gs3<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">2</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
<span style="color: #666666;">// this object will be intialized but the dtor will not be called</span>
GlobalStatic<span style="color: #000040;">*</span> gsp <span style="color: #000080;">=</span> <span style="color: #0000dd;">new</span> GlobalStatic<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">3</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span></pre></td></tr></table></div>

<p>Then, I added two lines of code to the main.m file:</p>
<pre>#include &lt;stdio.h&gt; // add this right below the #import &lt;UIKit/UIKit.h&gt;</pre>
<p>And then right at the top of the main() method I added:</p>
<pre>printf("main()\n");</pre>
<p>And that&#8217;s it. No compiler settings, nothing special, just adding the code to the project (though note that a fuller interaction between Obj-C and C++ will take a little more effort). So, what happens when you build and run this? Basically, the objects gs1, gs2 and gs3 and the pointer gsp will be created/allocated by the C++ runtime lib, and this will happen *before* the main entry to the program is called. In the debugger console (Run -&gt; Console) you will see the following output:</p>
<p><strong><code>GlobalStatic()<br />
GlobalStatic(1)<br />
GlobalStatic(2)<br />
GlobalStatic(3)<br />
main()</code> </strong></p>
<p>When you close down the application, the destructors for the objects gs1, gs2, and gs3 will be called, and you&#8217;ll see the following output on the console:</p>
<p><strong><code>~GlobalStatic(2)<br />
~GlobalStatic(1)<br />
~GlobalStatic(0)<br />
</code> </strong></p>
<p>Note! The destructor for the object pointed at by gsp is not called! This is expected, and this could potentially be a resource leak (though, in this case the runtime wil likely clean up the memory at least). It&#8217;s important to remember this though &#8211; if the GlobalStatic class had been expected to set some state in its destructor (in a database, for example), then that would never happen for that object.</p>
<p>There are actually many subtle (and potentially disasterous) side effects associated with the use of C++ global statics. There are ordering issues that can occur if one such object refers to another and on and on. Refer the C++ FAQ lite for details of some of the horrors (<a href="http://www.parashift.com/c++-faq-lite/ctors.html" target="_blank">http://www.parashift.com/c++-faq-lite/ctors.html</a> ). So, I am not holding up this technique as a panacea or even something that is highly recommended &#8211; you need to know what you are doing and even then tread very carefully. However, as s benchmark for the level of C++ support in the runtime, this is pretty good.</p>
<p>I will cover exceptions, RTTI and more in future articles (time permitting!). What this article has hopefully shown is that there are viable alternatives to using Obj-C on the iPhone and that if you are familiar with using C++ on pretty much any other mobile platform, it may be that the iPhone will pleasantly surprise you with what it <strong>does</strong> do, rather than by what it does <strong>not</strong> .</p>
<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><!-- Do not remove -->]]></content:encoded>
			<wfw:commentRss>http://iPhoneDeveloperTips.com/cpp/c-on-iphone-part-1.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>

