<?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>いちばんやさしいゲームの作り方 &#187; ヘッダファイル</title>
	<atom:link href="http://www.game-create.com/archives/tag/%e3%83%98%e3%83%83%e3%83%80%e3%83%95%e3%82%a1%e3%82%a4%e3%83%ab/feed" rel="self" type="application/rss+xml" />
	<link>http://www.game-create.com</link>
	<description>文系の人でも、数理学がわからない人でもゲームプログラミングをマスターできるブログ</description>
	<lastBuildDate>Tue, 08 Jun 2010 01:28:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>プログラミングの基礎知識を何かにたとえて説明するのは難しい</title>
		<link>http://www.game-create.com/archives/2053</link>
		<comments>http://www.game-create.com/archives/2053#comments</comments>
		<pubDate>Sun, 09 Aug 2009 16:05:46 +0000</pubDate>
		<dc:creator>Byerkut</dc:creator>
				<category><![CDATA[ゲームプログラミング]]></category>
		<category><![CDATA[ヘッダファイル]]></category>
		<category><![CDATA[メルマガ]]></category>
		<category><![CDATA[関数]]></category>

		<guid isPermaLink="false">http://www.game-create.com/?p=2053</guid>
		<description><![CDATA[メルマガではヘッダファイルを説明するのに名刺にたとえましたが、レストランのメニューにたとえている情報源がありまして、断然そっちの方がわかりやすいと感じました。

何かにたとえるのは教える側の「なんとか身近な物を想像しても [...]]]></description>
			<content:encoded><![CDATA[<p>メルマガではヘッダファイルを説明するのに名刺にたとえましたが、レストランのメニューにたとえている情報源がありまして、断然そっちの方がわかりやすいと感じました。</p>
<p><span id="more-2053"></span></p>
<p>何かにたとえるのは教える側の「なんとか身近な物を想像してもらうことで、意味・必要性・雰囲気を感じ取って欲しい」という意図があるのですが、下手するとかえってわかりづらくなります。使われ方が似ていても用途などに隔たりがあるともう伝わりづらいと思います。</p>
<p>レストランのメニューは良いですね。どんなものが頼めるのか？それはどんな名前か？セットはつけられるのか？焼き方は選べるのか？なんも言わなかったら何がついてくるのか？ヘッダファイル見て関数のインタフェースを調べるのに似ています。</p>
<p>ただ、メニューはコピーして配布するのに対し、ヘッダファイルはプロジェクト内ではひとつです。パソコンが違えばコピーして配布するのですが、こういう事情も伝わりづらさのひとつです。</p>
<p>難しいです。できることといえば「目的」に焦点を当てることくらいでしょうか。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.game-create.com/archives/2053/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>#include を正しく使う</title>
		<link>http://www.game-create.com/archives/532</link>
		<comments>http://www.game-create.com/archives/532#comments</comments>
		<pubDate>Sun, 03 Aug 2008 00:50:18 +0000</pubDate>
		<dc:creator>Byerkut</dc:creator>
				<category><![CDATA[ゲームプログラミング]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[ヘッダファイル]]></category>

		<guid isPermaLink="false">http://www.game-create.com/?p=532</guid>
		<description><![CDATA[#include には知っていると気分が良い作法がありますのでご紹介します。

C/C++ 標準ヘッダーや OS 提供のヘッダを読み込むときは  を使う
stdio や iostream など C/C++ が標準で持って [...]]]></description>
			<content:encoded><![CDATA[<p>#include には知っていると気分が良い作法がありますのでご紹介します。</p>
<p><span id="more-532"></span></p>
<h4>C/C++ 標準ヘッダーや OS 提供のヘッダを読み込むときは <> を使う</h4>
<p>stdio や iostream など C/C++ が標準で持っているヘッダを読み込む際には <> を使います。また、 windows.h などのように OS が提供するヘッダを読み込む際にも <> です。</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-prepro">#include </span><span class="hl-quotes">&lt;</span><span class="hl-string">cstdio</span><span class="hl-quotes">&gt;</span><span class="hl-prepro">
#include </span><span class="hl-quotes">&lt;</span><span class="hl-string">iostream</span><span class="hl-quotes">&gt;</span><span class="hl-prepro">
#include </span><span class="hl-quotes">&lt;</span><span class="hl-string">windows.h</span><span class="hl-quotes">&gt;</span></pre></div></div>
<h4>自作したヘッダを読み込むときは &#8220;&#8221; を使う</h4>
<p>自作したヘッダを読み込む際には &#8220;&#8221; を使います。もしかしたら <> を使っても正しく読み込めるかもしれませんが、可読性を考えて使い分けた方が良いと思います。</p>
<div class="hl-surround" ><div class="hl-main"><pre>#include &quot;background.h&quot;
#include &quot;player.h&quot;
#include &quot;enemy.h&quot;</pre></div></div>
<h4>ディレクトリの区切り文字にはスラッシュを使う</h4>
<p>Windows 派の方は要注意です。というのも #include に Windows のディレクトリ区切り文字である \ （円マーク、またはバックスラッシュ）を渡すことができないためです。おそらく \ を渡しても正しく動くとは思うのですが、規約的には間違っています。</p>
<div class="hl-surround" ><div class="hl-main"><pre>#include &lt;boost/shared_ptr.hpp&gt;
#include &quot;common/types.h&quot;</pre></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.game-create.com/archives/532/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>自分で作った関数の存在を他の関数やファイルに教える</title>
		<link>http://www.game-create.com/archives/414</link>
		<comments>http://www.game-create.com/archives/414#comments</comments>
		<pubDate>Fri, 04 Jul 2008 08:11:18 +0000</pubDate>
		<dc:creator>Byerkut</dc:creator>
				<category><![CDATA[ゲームプログラミング]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[プロトタイプ宣言]]></category>
		<category><![CDATA[ヘッダファイル]]></category>
		<category><![CDATA[関数]]></category>

		<guid isPermaLink="false">http://www.game-create.com/?p=414</guid>
		<description><![CDATA[自分で作った関数の存在を他の関数やファイルに教えるためには、関数のプロトタイプ宣言を記述します。

関数は基本的に自分以外の関数の存在を知りませんが、あらかじめプロトタイプ宣言をしておくと、他の関数の存在を知ることができ [...]]]></description>
			<content:encoded><![CDATA[<p>自分で作った関数の存在を他の関数やファイルに教えるためには、関数のプロトタイプ宣言を記述します。</p>
<p><span id="more-414"></span></p>
<p>関数は基本的に自分以外の関数の存在を知りませんが、あらかじめプロトタイプ宣言をしておくと、他の関数の存在を知ることができ、自分の関数の中から他の関数を使うことができるようになります。</p>
<p>関数のプロトタイプ宣言は、その関数を使いたい場所より前に書く必要がありますが、その書き方は非常に簡単です。関数の定義からブロックを省略するだけで済みます。</p>
<p>たとえば次のような関数があったとき…</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-types">int </span><span class="hl-identifier">someFunction</span><span class="hl-brackets">(</span><span class="hl-types">int </span><span class="hl-identifier">arg1</span><span class="hl-code">, </span><span class="hl-types">char</span><span class="hl-code"> *</span><span class="hl-identifier">arg2</span><span class="hl-brackets">)
{
  </span><span class="hl-types">int </span><span class="hl-identifier">result</span><span class="hl-code"> = </span><span class="hl-identifier">arg1</span><span class="hl-code"> + </span><span class="hl-identifier">atoi</span><span class="hl-brackets">(</span><span class="hl-identifier">arg2</span><span class="hl-brackets">)</span><span class="hl-code">;
  </span><span class="hl-reserved">return </span><span class="hl-identifier">result</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></pre></div></div>
<p>プロトタイプ宣言は次のようになります。</p>
<div class="hl-surround" style="height:16.8px;"><div class="hl-main"><pre><span class="hl-types">int </span><span class="hl-identifier">someFunction</span><span class="hl-brackets">(</span><span class="hl-types">int </span><span class="hl-identifier">arg1</span><span class="hl-code">, </span><span class="hl-types">char</span><span class="hl-code"> *</span><span class="hl-identifier">arg2</span><span class="hl-brackets">)</span><span class="hl-default">;</span></pre></div></div>
<p>実際に main() 関数などから、この someFunction() 関数を使いたい場合は次のように書きます。</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">// まずプロトタイプ宣言を書く
</span><span class="hl-types">int </span><span class="hl-identifier">someFunction</span><span class="hl-brackets">(</span><span class="hl-types">int </span><span class="hl-identifier">arg1</span><span class="hl-code">, </span><span class="hl-types">char</span><span class="hl-code"> *</span><span class="hl-identifier">arg2</span><span class="hl-brackets">)</span><span class="hl-default">;

</span><span class="hl-comment">// someFunction() を使う main() 関数を書く
</span><span class="hl-types">int </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-types">int </span><span class="hl-identifier">argc</span><span class="hl-code">, </span><span class="hl-types">char</span><span class="hl-code"> *</span><span class="hl-identifier">argv</span><span class="hl-brackets">[])
{
  </span><span class="hl-types">int </span><span class="hl-identifier">arg1</span><span class="hl-code"> = </span><span class="hl-number">10</span><span class="hl-code">;
  </span><span class="hl-types">char</span><span class="hl-code"> *</span><span class="hl-identifier">arg2</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">30</span><span class="hl-quotes">&quot;</span><span class="hl-code">;

  </span><span class="hl-reserved">return </span><span class="hl-identifier">someFunction</span><span class="hl-brackets">(</span><span class="hl-identifier">arg1</span><span class="hl-code">, </span><span class="hl-identifier">arg2</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}

</span><span class="hl-comment">// someFunction() 関数の本体を書く
// 必ずしもここに書く必要はなく、別のファイルでも問題なし
// ただし、その場合でもプロトタイプ宣言は必要になる
</span><span class="hl-types">int </span><span class="hl-identifier">someFunction</span><span class="hl-brackets">(</span><span class="hl-types">int </span><span class="hl-identifier">arg1</span><span class="hl-code">, </span><span class="hl-types">char</span><span class="hl-code"> *</span><span class="hl-identifier">arg2</span><span class="hl-brackets">)
{
  </span><span class="hl-types">int </span><span class="hl-identifier">result</span><span class="hl-code"> = </span><span class="hl-identifier">arg1</span><span class="hl-code"> + </span><span class="hl-identifier">atoi</span><span class="hl-brackets">(</span><span class="hl-identifier">arg2</span><span class="hl-brackets">)</span><span class="hl-code">;
  </span><span class="hl-reserved">return </span><span class="hl-identifier">result</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></pre></div></div>
<p>プロトタイプ宣言はヘッダファイルの中に書くのが一般的です。 #include を使うことで大量の関数のプロトタイプ宣言を一気にインポートできるからです。一番わかりやすい例が windows.h です。 Windows のプログラムを開発する場合には必ず #include <windows.h> と書いてきましたが、この windows.h の中では膨大な数のプロトタイプ宣言が書いてあります。これは Windows が持っている関数の存在を自分のソースコードに教えていることに他なりません。</p>
<p>ちなみに、関数を使う前に関数の定義が来ている場合はプロトタイプ宣言が不要です。たとえば次のコードは問題なく動作します。</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">// someFunction() 関数の定義を前もって書いておき、
// プロトタイプ宣言は書かない
</span><span class="hl-types">int </span><span class="hl-identifier">someFunction</span><span class="hl-brackets">(</span><span class="hl-types">int </span><span class="hl-identifier">arg1</span><span class="hl-code">, </span><span class="hl-types">char</span><span class="hl-code"> *</span><span class="hl-identifier">arg2</span><span class="hl-brackets">)
{
  </span><span class="hl-types">int </span><span class="hl-identifier">result</span><span class="hl-code"> = </span><span class="hl-identifier">arg1</span><span class="hl-code"> + </span><span class="hl-identifier">atoi</span><span class="hl-brackets">(</span><span class="hl-identifier">arg2</span><span class="hl-brackets">)</span><span class="hl-code">;
  </span><span class="hl-reserved">return </span><span class="hl-identifier">result</span><span class="hl-code">;
</span><span class="hl-brackets">}

</span><span class="hl-comment">// someFunction() にはプロトタイプ宣言がないが、
// すでに上で定義済みなので問題なく使うことができる
</span><span class="hl-types">int </span><span class="hl-identifier">main</span><span class="hl-brackets">(</span><span class="hl-types">int </span><span class="hl-identifier">argc</span><span class="hl-code">, </span><span class="hl-types">char</span><span class="hl-code"> *</span><span class="hl-identifier">argv</span><span class="hl-brackets">[])
{
  </span><span class="hl-types">int </span><span class="hl-identifier">arg1</span><span class="hl-code"> = </span><span class="hl-number">10</span><span class="hl-code">;
  </span><span class="hl-types">char</span><span class="hl-code"> *</span><span class="hl-identifier">arg2</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">30</span><span class="hl-quotes">&quot;</span><span class="hl-code">;

  </span><span class="hl-reserved">return </span><span class="hl-identifier">someFunction</span><span class="hl-brackets">(</span><span class="hl-identifier">arg1</span><span class="hl-code">, </span><span class="hl-identifier">arg2</span><span class="hl-brackets">)</span><span class="hl-code">;
</span><span class="hl-brackets">}</span></pre></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.game-create.com/archives/414/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ひとつのコンパイル単位で同じヘッダファイルを２度 #include しないようにする</title>
		<link>http://www.game-create.com/archives/413</link>
		<comments>http://www.game-create.com/archives/413#comments</comments>
		<pubDate>Thu, 03 Jul 2008 07:19:24 +0000</pubDate>
		<dc:creator>Byerkut</dc:creator>
				<category><![CDATA[ゲームプログラミング]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[イディオム]]></category>
		<category><![CDATA[プリプロセッサ]]></category>
		<category><![CDATA[ヘッダファイル]]></category>

		<guid isPermaLink="false">http://www.game-create.com/?p=413</guid>
		<description><![CDATA[ひとつのコンパイル単位で同じヘッダファイルを２度 #include しないようにするためには、プリプロセッサを使います。

そもそも、なぜコンパイル単位で同じヘッダを２度 #include してはならないのかというと、無 [...]]]></description>
			<content:encoded><![CDATA[<p>ひとつのコンパイル単位で同じヘッダファイルを２度 #include しないようにするためには、プリプロセッサを使います。</p>
<p><span id="more-413"></span></p>
<p>そもそも、なぜコンパイル単位で同じヘッダを２度 #include してはならないのかというと、無限 #include に陥ってしまったり、シンボルが重複してしまうからです。たとえば this.h というヘッダファイルの中で #include &#8220;this.h&#8221; と書いてしまうと this.h が this.h を #include して、さらに #include された this.h が this.h を #include しで…となってしまい、コンパイルがいつまで経っても終わらなくなります。シンボルの重複とは話が簡単です。要は同じ名前は２回使えませんということです。たとえば構造体や関数や変数の名前は決められた範囲の中で一意である必要があるのです。</p>
<p>なにやら訳がわかりませんが、幸いなことに対処方法は非常に簡単です。ヘッダファイルに定型句を書くだけです。</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-prepro">#ifndef </span><span class="hl-identifier">THIS_HEADER_INCLUDED__</span><span class="hl-prepro">
#define </span><span class="hl-identifier">THIS_HEADER_INCLUDED__</span><span class="hl-prepro">

</span><span class="hl-mlcomment">/* ここにヘッダファイルの中身を書く */

</span><span class="hl-prepro">#endif</span></pre></div></div>
<p>THIS_HEADER_INCLUDED__ はヘッダファイルごとに違う文字列を入れてください。好きな名前をつけていただいて問題ありませんが、他のファイルと重複しないようにする必要があります。ファイル名から考えるのが楽かと思います。こう書いておくとたとえば次のようなコードを書いても問題なくコンパイルが通るようになります。</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">// わざと何回も #include してみる
</span><span class="hl-prepro">#include </span><span class="hl-quotes">&quot;</span><span class="hl-string">this.h</span><span class="hl-quotes">&quot;</span><span class="hl-prepro">
#include </span><span class="hl-quotes">&quot;</span><span class="hl-string">this.h</span><span class="hl-quotes">&quot;</span><span class="hl-prepro">
#include </span><span class="hl-quotes">&quot;</span><span class="hl-string">this.h</span><span class="hl-quotes">&quot;</span><span class="hl-prepro">
#include </span><span class="hl-quotes">&quot;</span><span class="hl-string">this.h</span><span class="hl-quotes">&quot;</span><span class="hl-prepro">
#include </span><span class="hl-quotes">&quot;</span><span class="hl-string">this.h</span><span class="hl-quotes">&quot;</span><span class="hl-prepro">
</span><span class="hl-comment">// 問題なし</span></pre></div></div>
<p>ここまで極端なソースにはならないと思いますが、同じような意味のことは十分起こりえます。ヘッダファイルを書く場合は #ifndef #define #endif のセットで覚えておきましょう。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.game-create.com/archives/413/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>警告レベルがあわないヘッダのインクルードで警告を抑制する方法</title>
		<link>http://www.game-create.com/archives/411</link>
		<comments>http://www.game-create.com/archives/411#comments</comments>
		<pubDate>Tue, 01 Jul 2008 13:31:27 +0000</pubDate>
		<dc:creator>Byerkut</dc:creator>
				<category><![CDATA[ゲームプログラミング]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[コンパイラ]]></category>
		<category><![CDATA[プラグマ]]></category>
		<category><![CDATA[ヘッダファイル]]></category>

		<guid isPermaLink="false">http://www.game-create.com/?p=411</guid>
		<description><![CDATA[警告レベルを最大にしたいのに #include するヘッダで警告に遭遇してしまう場合に、そこだけ警告を抑制する方法です。また、ヘッダが勝手に警告を抑制している場合にも使えます。

// 警告設定をスタックに積む
#pr [...]]]></description>
			<content:encoded><![CDATA[<p>警告レベルを最大にしたいのに #include するヘッダで警告に遭遇してしまう場合に、そこだけ警告を抑制する方法です。また、ヘッダが勝手に警告を抑制している場合にも使えます。</p>
<p><span id="more-411"></span></p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">// 警告設定をスタックに積む
</span><span class="hl-prepro">#pragma </span><span class="hl-identifier">warning</span><span class="hl-brackets">(</span><span class="hl-identifier">push</span><span class="hl-brackets">)</span><span class="hl-prepro">

</span><span class="hl-comment">// uncorporatable_header.h で次の警告を抑制する
</span><span class="hl-prepro">#pragma </span><span class="hl-identifier">warning</span><span class="hl-brackets">(</span><span class="hl-identifier">disable</span><span class="hl-code">: </span><span class="hl-number">4000</span><span class="hl-brackets">)</span><span class="hl-prepro">
#pragma </span><span class="hl-identifier">warning</span><span class="hl-brackets">(</span><span class="hl-identifier">disable</span><span class="hl-code">: </span><span class="hl-number">4001</span><span class="hl-brackets">)</span><span class="hl-prepro">

</span><span class="hl-comment">// このヘッダで 4000 と 4001 の警告が抑制される
</span><span class="hl-prepro">#include </span><span class="hl-quotes">&quot;</span><span class="hl-string">uncorporatable_header.h</span><span class="hl-quotes">&quot;</span><span class="hl-prepro">

</span><span class="hl-comment">// 警告設定を元に戻す
</span><span class="hl-prepro">#pragma </span><span class="hl-identifier">warning</span><span class="hl-brackets">(</span><span class="hl-identifier">pop</span><span class="hl-brackets">)</span></pre></div></div>
<p>#pragma pop と #pragma push は、内部でプラグマを変更しているヘッダを読み込む際に、後のコンパイルに影響を与えないようにする目的にも使えます。かなり便利です。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.game-create.com/archives/411/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>ヘッダファイルの役割</title>
		<link>http://www.game-create.com/archives/399</link>
		<comments>http://www.game-create.com/archives/399#comments</comments>
		<pubDate>Fri, 20 Jun 2008 12:26:27 +0000</pubDate>
		<dc:creator>Byerkut</dc:creator>
				<category><![CDATA[ゲームプログラミング]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[ヘッダファイル]]></category>
		<category><![CDATA[定数]]></category>
		<category><![CDATA[構造体]]></category>
		<category><![CDATA[言語仕様]]></category>
		<category><![CDATA[関数]]></category>

		<guid isPermaLink="false">http://www.game-create.com/?p=399</guid>
		<description><![CDATA[ヘッダファイルは、ある *.cpp が持っている関数や型を、他のファイルから使えるようにするために *.cpp の概要を外部へ公開する名刺のようなファイルです。
ヘッダファイルは *.cpp ファイルに対してひとつ *. [...]]]></description>
			<content:encoded><![CDATA[<p>ヘッダファイルは、ある *.cpp が持っている関数や型を、他のファイルから使えるようにするために *.cpp の概要を外部へ公開する名刺のようなファイルです。</p>
<p>ヘッダファイルは *.cpp ファイルに対してひとつ *.h という形で作りますが、その内容として次のような情報を記述します。</p>
<ul>
<li>その *.cpp が外部に対してどんな型を提供しているか？</li>
<li>その *.cpp が外部に対してどんな関数を提供しているか？</li>
<li>#include ガード</li>
</ul>
<p><span id="more-399"></span></p>
<h4>その *.cpp が外部に対してどんな型を提供しているか？</h4>
<p>*.cpp が外部に公開している型を他のファイルに教えるための記述です。 class や typedef や enum などを書くことができます。</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-comment">// 構造体の定義
</span><span class="hl-types">typedef struct </span><span class="hl-brackets">{
  </span><span class="hl-comment">// …
</span><span class="hl-brackets">} </span><span class="hl-identifier">SampleStructure</span><span class="hl-default">;

</span><span class="hl-comment">// クラスの定義
</span><span class="hl-types">class </span><span class="hl-identifier">SampleClass
</span><span class="hl-brackets">{
  </span><span class="hl-comment">// …
</span><span class="hl-brackets">}

</span><span class="hl-comment">// 列挙型の定義
</span><span class="hl-types">typedef enum </span><span class="hl-brackets">{
  </span><span class="hl-comment">// …
</span><span class="hl-brackets">} </span><span class="hl-identifier">SampleEnum</span><span class="hl-default">;</span></pre></div></div>
<h4>その *.cpp が外部に対してどんな関数を提供しているか？</h4>
<p>*.cpp が外部に公開している関数を他のファイルに教えるための記述です。他のファイルからはこの *.h ファイルを #include することで *.cpp が提供している関数を使うことができるようになります。関数を外部に公開するためには、関数の名前・引数・戻り値を次のように記述します。</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-types">void </span><span class="hl-identifier">InitializePlayer</span><span class="hl-brackets">(</span><span class="hl-identifier">HWND hWindow</span><span class="hl-code">, </span><span class="hl-identifier">HDC hBackBuffer</span><span class="hl-brackets">)</span><span class="hl-default">;
</span><span class="hl-types">void </span><span class="hl-identifier">FinalizePlayer</span><span class="hl-brackets">(</span><span class="hl-identifier">HWND hWindow</span><span class="hl-code">, </span><span class="hl-identifier">HDC hBackBuffer</span><span class="hl-brackets">)</span><span class="hl-default">;
</span><span class="hl-types">void </span><span class="hl-identifier">UpdatePlayer</span><span class="hl-brackets">(</span><span class="hl-identifier">HWND hWindow</span><span class="hl-code">, </span><span class="hl-identifier">HDC hBackBuffer</span><span class="hl-brackets">)</span><span class="hl-default">;
</span><span class="hl-types">void </span><span class="hl-identifier">DrawPlayer</span><span class="hl-brackets">(</span><span class="hl-identifier">HWND hWindow</span><span class="hl-code">, </span><span class="hl-identifier">HDC hBackBuffer</span><span class="hl-brackets">)</span><span class="hl-default">;</span></pre></div></div>
<h4>#include ガード</h4>
<p>同じ *.h ファイルを２回以上 #include してしまうと「同じ関数名が使われています」というようなエラーになってしまいます。これを回避するために #include ガードを記述します。</p>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-prepro">#ifndef </span><span class="hl-identifier">SAMPLE_HEADER_INCLUDED__</span><span class="hl-prepro">
#define </span><span class="hl-identifier">SAMPLE_HEADER_INCLUDED__</span><span class="hl-prepro">

</span><span class="hl-comment">// ここにヘッダファイルの中身を書きます

</span><span class="hl-prepro">#endif</span></pre></div></div>
<p>#include ガードは任意の文字列（前述の例では SAMPLE_HEADER_INCLUDED__ ）を用いて、「その文字列が初めて使われるか？」「すでに使われていないか？」を検証し、初めて使われている場合は *.h ファイルをインクルードし、すでに使われている場合は #include をスキップするようにします。これによって同じ *.h ファイルが２回以上 #include されないようになります。任意の文字列（前述の例では SAMPLE_HEADER_INCLUDED__ ）は好きな文字列を指定することができますが、ファイルごとに違う文字列を指定する必要があります。また、アンダーバーで始まる名前は使用できません。</p>
<h4>完全なヘッダファイルの例</h4>
<div class="hl-surround" ><div class="hl-main"><pre><span class="hl-prepro">#ifndef </span><span class="hl-identifier">SAMPLE_HEADER_INCLUDED__</span><span class="hl-prepro">
#define </span><span class="hl-identifier">SAMPLE_HEADER_INCLUDED__</span><span class="hl-prepro">

</span><span class="hl-comment">// 型を定義する例
</span><span class="hl-types">typedef struct </span><span class="hl-brackets">{
  </span><span class="hl-types">int </span><span class="hl-identifier">x</span><span class="hl-code">;
  </span><span class="hl-types">int </span><span class="hl-identifier">y</span><span class="hl-code">;
  </span><span class="hl-types">int </span><span class="hl-identifier">width</span><span class="hl-code">;
  </span><span class="hl-types">int </span><span class="hl-identifier">height</span><span class="hl-code">;
  </span><span class="hl-types">int </span><span class="hl-identifier">life</span><span class="hl-code">;
</span><span class="hl-brackets">} </span><span class="hl-identifier">Player</span><span class="hl-default">, *</span><span class="hl-identifier">pPlayer</span><span class="hl-default">;

</span><span class="hl-comment">// 関数を定義する例
</span><span class="hl-types">void </span><span class="hl-identifier">InitializePlayer</span><span class="hl-brackets">(</span><span class="hl-identifier">HWND hWindow</span><span class="hl-code">, </span><span class="hl-identifier">HDC hBackBuffer</span><span class="hl-brackets">)</span><span class="hl-default">;
</span><span class="hl-types">void </span><span class="hl-identifier">FinalizePlayer</span><span class="hl-brackets">(</span><span class="hl-identifier">HWND hWindow</span><span class="hl-code">, </span><span class="hl-identifier">HDC hBackBuffer</span><span class="hl-brackets">)</span><span class="hl-default">;
</span><span class="hl-types">void </span><span class="hl-identifier">UpdatePlayer</span><span class="hl-brackets">(</span><span class="hl-identifier">HWND hWindow</span><span class="hl-code">, </span><span class="hl-identifier">HDC hBackBuffer</span><span class="hl-brackets">)</span><span class="hl-default">;
</span><span class="hl-types">void </span><span class="hl-identifier">DrawPlayer</span><span class="hl-brackets">(</span><span class="hl-identifier">HWND hWindow</span><span class="hl-code">, </span><span class="hl-identifier">HDC hBackBuffer</span><span class="hl-brackets">)</span><span class="hl-default">;

</span><span class="hl-prepro">#endif</span></pre></div></div>
]]></content:encoded>
			<wfw:commentRss>http://www.game-create.com/archives/399/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>ヘッダファイルに書くことと書かないこと</title>
		<link>http://www.game-create.com/archives/111</link>
		<comments>http://www.game-create.com/archives/111#comments</comments>
		<pubDate>Thu, 11 Oct 2007 08:36:21 +0000</pubDate>
		<dc:creator>Byerkut</dc:creator>
				<category><![CDATA[ゲームプログラミング]]></category>
		<category><![CDATA[C++]]></category>
		<category><![CDATA[ヘッダファイル]]></category>
		<category><![CDATA[まとめ]]></category>

		<guid isPermaLink="false">http://www.game-create.com/?p=111</guid>
		<description><![CDATA[ヘッダファイルに書くことと書かないことをまとめてみました。

書くこと

依存ヘッダの読み込み &#8211; #include
定数の定義 &#8211; #define
構造体の定義 &#8211; struct
型 [...]]]></description>
			<content:encoded><![CDATA[<p>ヘッダファイルに書くことと書かないことをまとめてみました。</p>
<p><span id="more-111"></span></p>
<h4>書くこと</h4>
<ul>
<li>依存ヘッダの読み込み &#8211; #include</li>
<li>定数の定義 &#8211; #define</li>
<li>構造体の定義 &#8211; struct</li>
<li>型の定義 &#8211; typedef</li>
<li>関数のプロトタイプ</li>
</ul>
<h4>書かないこと</h4>
<ul>
<li>静的グローバル変数の定義</li>
<li>静的関数のプロトタイプ</li>
</ul>
<p>随時加筆修正します。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.game-create.com/archives/111/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

