C言語のdefineマクロは便利
■ コピペ関数をなくす
x##,#xの使い方のサンプルですな
#include <iostream> #define DEF_HOGE(x) void x##_hoge() {std::cerr << #x", hoge!" << std::endl;} DEF_HOGE(foo) DEF_HOGE(bar) int main () { foo_hoge(); bar_hoge(); return 0; }
実行すると
foo, hoge!
bar, hoge!
特に同じようなメソッドでオーバーロードしたものも作らないといけないときとか激しく便利
#include <iostream> #define DEF_HOGE(x) \ static void x##_hoge(int a); \ static void x##_hoge(double y); #define DEF_HOGE_IMPL(x) \ void Hoge::x##_hoge(int a) \ { \ std::cerr << "int " << a << std::endl; \ } \ void Hoge::x##_hoge(double a) \ { \ std::cerr << "double " << a << std::endl; \ } struct Hoge { DEF_HOGE(foo) DEF_HOGE(bar) }; DEF_HOGE_IMPL(foo) DEF_HOGE_IMPL(bar) int main() { Hoge::foo_hoge(10); Hoge::bar_hoge(3.5); return 0; }
実行すると
int 10
double 3.5
Referrer
1.531 sec
- 10: http://makingx.net/blog/2008/08/14/c%E8%A8%80%E8%AA%9E%E3%81%AEdefine%E3%83%9E%E3%82%AF%E3%83%AD%E3%81%AE%E3%81%A8%E3%81%8B/
- 4: http://makingx.net/blog/2008/08/14/c%e8%a8%80%e8%aa%9e%e3%81%aedefine%e3%83%9e%e3%82%af%e3%83%ad%e3%81%ae%e3%81%a8%e3%81%8b/
- 3: http://www.google.co.jp/search?hl=ja&lr=&q=define+%E3%83%9E%E3%82%AF%E3%83%AD+C%2B%2B&revid=1284413022&sa=X&oi=revisions_inline&resnum=0&ct=top-revision&cd=2
- 2: http://www.google.co.jp/search?hl=ja&safe=active&rlz=1T4DBJP_jaJP270JP270&q=%E3%83%9E%E3%82%AF%E3%83%AD%E3%80%80C%2B%2B&start=20&sa=N
- 2: http://www.google.co.jp/

