C++

高階メタ関数

C++テンプレートテクニック 4-6 ここは自分なりのサンプルが全然思いつかなかった。とりあえずまったく意味のないシンプルなコードだけ書いてみた。 #include <iostream> using std::cout; using std::endl; template<class T> struct bar { static const bool value = T::value</class></iostream>…

メタ関数で型を変換する

C++テンプレートテクニック 4-3 テンプレートメタプログラミング - (void*)Pないとの続きです。今度は型を変換するメタ関数の登場です。 #include <iostream> using std::cout; using std::endl; template<class T> struct types { typedef T type; }; int main () { types<int>::typ</int></class></iostream>…

テンプレートメタプログラミング

C++テンプレートテクニック 4-1、4-2 テンプレートのインスタンス化を利用して、コンパイル時にプログラミングをすることをテンプレートメタプログラミングという。 #include <iostream> using std::cout; using std::endl; template<int N> class CClass { public: static co</int></iostream>…

パラメータ化継承

C++テンプレートテクニック 3-6 単に基底クラス名をテンプレート指定するという話ですが template<class T> class Foo : public T { } 何か色々できそうな気はするのですが、具体的な利用法が思い浮かばないですね。</class>

タグ・ディスパッチ

C++テンプレートテクニック 3-5 簡単に言えばオーバーロードを利用した関数の呼び分け処理といった感じでしょうか。シンプルな例を考えてみました。 #include <iostream> using std::cout; using std::endl; // ディスパッチ用のタグ名 struct tag_a {}; struct tag_b </iostream>…

Vector等も渡せる配列操作関数を作成する

C++テンプレートテクニック 3-3 とりあえず例としてfindを作ってみます。 #include <iostream> using std::cout; using std::endl; template<class T> T* myfind (T* first,T* last, T value) { T* p=first; for (; p != last; ++p) { if ( *p == value ) { break; } } return p</class></iostream>…

テンプレート・テンプレート・パラメータ

C++テンプレートテクニック 2-8 template<class T> class CClass { T<int> m_x; }; $ cl /W4 /EHsc main.cpp Microsoft(R) 32-bit C/C++ Optimizing Compiler Version 15.00.30729.01 for 80x86 Copyright (C) Microsoft Corporation. All rights reserved. main.cpp main.</int></class>…

template限定子

C++テンプレートテクニック 2-6 template<class T> int foo(T x,int n) { return x.get<3>(n); } 上記のようなテンプレートの場合、「x.get(n)」は変数xのgetメンバ関数の呼び出しを期待しているわけなのだが、変数xのgetメンバ変数と3を比較するという処理、つまり「</class>…

typenameキーワード

C++テンプレートテクニック 2-6 テンプレートの曖昧さ回避のために使用するらしい。 #include <iostream> using std::cout; using std::endl; struct ClassA { typedef int result; }; struct ClassB { static const int result = 5; }; int p; template<class T> void func (T)</class></iostream>…

コンストラクタテンプレート

C++テンプレートテクニック 2-5 コンストラクタテンプレートという名前がついてますが、要はクラステンプレートとメンバ関数テンプレートの合わせ技ですね。 関連 http://d.hatena.ne.jp/pknight/20090805/1249471903

テンプレート引数にポインタを渡す

C++テンプレートテクニック 2-4 テンプレート引数にはコンパイル時に値が確定しているグローバル変数や関数のポインタも渡すことができます。 #include <iostream> using std::cout; using std::endl; template<class T,void (T::*M)()> void func (T obj) { (obj.*M)(); } class CClass { publ</class></iostream>…

テンプレートと参照渡しを利用して配列の要素数を取得する

C++テンプレートテクニック 2-4 #include <iostream> using std::cout; using std::endl; template<class T,int N> int size(T (&)[N]) { return N; } int main () { int a[] = {3,4,5,6}; // 配列aの要素数を表示 cout << size(a) << endl; return 0; } $ main 4配列の参照渡しをした</class></iostream>…

配列のポインタ渡しと参照渡しの違い

C++

ポインタ渡しとは以下のような処理です。 #include <iostream> using std::cout; using std::endl; // ポインタ渡し void func(int *a) { cout << a[0] << endl; } int main () { int a[] = {3,4,5,6}; func(a); } また、以下のようにも書くことができます。 void func</iostream>…

明示的なインスタンス化の注意点

C++テンプレートテクニック 2-2 さて、以下のコードはコンパイルできるでしょうか? #include <iostream> using std::cout; using std::endl; template<class T> class CClass { public: T m_val; CClass (T val) { m_val = val; } void Set (T val) { m_val = val; } T Get () </class></iostream>…

ループを使わずに配列の順序を逆にする

C++

プログラマーの力量を見極める--面接官になったら尋ねるべき質問実例集 - ZDNet Japan ループを使わずに配列の順序を逆にする。 という件があったのでチャレンジしてみた。 #include <iostream> #define ARRAY_NUM(a) (sizeof(a)/sizeof(a[0])) using std::cout; using</iostream>…

練習用その2 アフィン変換に対応した2×2(実際は3×3)行列クラス

前回を踏まえて新しく実装。大分マシになってきた。 #include <cmath> #include "vector2.h" // アフィン変換に対応した2×2(実際は3×3)行列クラス namespace ddp { static const float PI = 6*static_cast<float>(asin( 0.5 )); class Matrix2 { private: public: float </float></cmath>…

まずは練習用に2×2の行列クラス

行列クラスを自分で作ってみる。まずは前提知識無しで作った最初のバージョンを公開 #include "vector2.h" namespace ddp { class Matrix2 { public: Vector2 v1; Vector2 v2; Matrix2(Vector2 v1,Vector2 v2) { this->v1 = v1; this->v2 = v2; } // ベクト…

ベクトルクラス

C++

ゲーム3D数学本を参考にベクトルクラスの作成。とりあえず2Dプログラム用なのでVector2とした。 #include <math.h> namespace ddp { class Vector2 { public: // 点。隠蔽する必要性が無いのでpublicで提供 float x; float y; // コンストラクタ。明示的な初期化は行</math.h>…

switch文のcaseでのブロック

C C++

switch文のcaseの中で変数宣言する場合は注意が必要です。 #include <iostream> using std::cout; using std::endl; int main () { switch(1) { case 1: int i = 0; break; case 2: int i = 0; break; } return 0; } $ cl /W4 /EHsc main.cpp Microsoft(R) 32-bit C/C+</iostream>…

上下左右をループで得るコード

C++

今日見たコードで凄いのがあった。 #include <iostream> using std::cout; using std::endl; int main () { for (int x = -1, y = 0, i = 0; i < 4; x += y, y = x - y, x = y - x, ++i) { cout << "x " << x << " y " << y << endl; } return 0; } $ main x -1 y 0 x </iostream>…

ベクトルクラスの設計

C++

ゲーム3D数学本 5章ここは凄くためになる。盲目的な隠蔽化は避けるべき。違和感はあったんだけどハッキリ言及があったので助かった。やはりxやyといった座標を持つだけのメンバ変数はprivateにするよりpublicにした方が良い。アクセサを用意するメリットが薄…

可変長配列メンバ

ロベールのC++入門講座 16-14クラスのメンバ変数の配列の要素数を可変長にするテクニックの話です。まず配列の要素を一つだけで宣言しておきます。 class CSample { public: int m_int; char m_char[1]; }; そしてこのクラスを定義する際に、クラスのサイズ…

匿名共用体

ロベールのC++入門講座 16-02メンバ変数に匿名共用体を用いれば、メモリを共有するメンバ変数を定義できます。 #include <iostream> using std::cout; using std::endl; class CSample { public: union { int m_int; char m_char[4]; }; }; int main () { CSample obj;</iostream>…

DirectX用のスマートポインタの作成

DirectX関連のオブジェクトを使用する場合に便利なスマートポインタを自作しました。 // -- smart_ptr.h -- #include <memory> namespace ddp { template<class T> class com_ptr { private: T* m_pComObj; public: explicit com_ptr (T* comObj = NULL) { m_pComObj = comObj</class></memory>…

アロー演算子のオーバーロードの注意点

C++

アロー演算子をオーバーロードして別のオブジェクトのポインタを返す処理があるとします。以下のような感じです。 #include <iostream> using std::cout; using std::endl; class CMember { public: void output () { cout << "CMember::output" << endl; } }; class C</iostream>…

キャスト演算子のオーバーロード

C++

http://homepage2.nifty.com/well/Operator.html#typeキャスト演算子もオーバーロードが可能です。 #include <iostream> using std::cout; using std::endl; class CSample { public: // intへのキャストのオーバーロード operator int () { return 100; } }; int main</iostream>…

メンバ変数ポインタ

ロベールのC++入門講座 13-17メンバ関数のポインタは以前勉強しました。実はメンバ変数もポインタを取ることができます。 #include <iostream> using std::cout; using std::endl; class CSample { public: int m_x; int m_y; CSample(int x,int y) { m_x = x; m_y = y</iostream>…

現状の見直し

やはり基礎の基礎をしっかりやらないと何も作れそうに無いので地盤固めに専念します。まずはスマートポインタを用いたライブラリ設計へ。またDirectXのCOM用のスマートポインタも実装しないといけません。何をやるにも基礎が重要。あらためて良く分かりまし…

スマートポインタ

C++

今日はスマートポインタについて学んでみたいと思います。newで確保した変数はdeleteで解放してあげないといけません。とはいえプログラマーは人間なのでdeleteを良く忘れてしまいます。ある関数の戻り値にnewした変数を返す場合があると、その関数がnewで確…

ある座標に向かって移動させる処理

前回の記事で「画面の中央に向かって移動する処理も追加してみました。」とさらりと言いましたが、実はこれが結構難しい処理だったりします。ある座標に向かって移動させるということは、今いる座標と目標となる座標の角度を求めて、その方向にX座標とY座標…