「トップページ/CPP」の編集履歴(バックアップ)一覧はこちら
トップページ/CPP - (2007/10/09 (火) 10:59:49) の1つ前との変更点
追加された行は緑色になります。
削除された行は赤色になります。
*C++一般
#contents()
*開発環境
GCC
Boost
----
*構造体メンバの先頭からのオフセットを得る
#include <stddef.h>
size_t offsetof( type, member);
----
*コールバックの実装(Boost)
Runメソッドを呼ぶと、testEventに登録された関数が呼ばれる。
testEventメンバをpublicにせず、SetTestEventメソッド経由にする理由は、外部からイベントハンドラを呼ぶことが出来てしまうからである。SetTestEventメソッドの引数にNULLを渡すと、イベントハンドラをクリアする事が出来る。
// 呼び出し元
class aa {
// イベントハンドラ(イベント発生時に呼び出す関数のアドレス)
protected: boost::function<string (int a,int b)> testEvent;
// 内部からイベントハンドラを呼び出す為のラッパー
protected: virtual string OnTestEvent(int a, int b) {
string retstr;
if (testEvent!=NULL)
retstr=testEvent(a,b);
return retstr;
}
// イベントハンドラを登録する
public: void SetTestEvent(boost::function<string (int a,int b)> func) {
testEvent = func;
}
public: void Run(void) {
cout << "[" << OnTestEvent(5,8) << "]" << endl;
}
}
// 呼び出し先
class bb {
protected: string TestEventFunc(int a, int b) {
return boost::io::str(boost::format("a+b=%d") % a+b);
}
}
//
int main(int, char**) {
aa AA;
bb BB;
AA.SetTestEvent( boost::bind(&bb::TestEventFunc, &BB, _1, _2);
AA.Run();
return 0;
}
*C++一般
#contents()
*開発環境
GCC
Boost
----
*16進文字列を数値にする
string str = "0xcdef";
int a = 0;
try {
a = boost::lexical_cast<int>(str);
}
catch(boost::bad_lexical_cast&) {
;
}
----
*構造体メンバの先頭からのオフセットを得る
#include <stddef.h>
size_t offsetof( type, member);
----
*コールバックの実装(Boost)
Runメソッドを呼ぶと、testEventに登録された関数が呼ばれる。
testEventメンバをpublicにせず、SetTestEventメソッド経由にする理由は、外部からイベントハンドラを呼ぶことが出来てしまうからである。SetTestEventメソッドの引数にNULLを渡すと、イベントハンドラをクリアする事が出来る。
// 呼び出し元
class aa {
// イベントハンドラ(イベント発生時に呼び出す関数のアドレス)
protected: boost::function<string (int a,int b)> testEvent;
// 内部からイベントハンドラを呼び出す為のラッパー
protected: virtual string OnTestEvent(int a, int b) {
string retstr;
if (testEvent!=NULL)
retstr=testEvent(a,b);
return retstr;
}
// イベントハンドラを登録する
public: void SetTestEvent(boost::function<string (int a,int b)> func) {
testEvent = func;
}
public: void Run(void) {
cout << "[" << OnTestEvent(5,8) << "]" << endl;
}
}
// 呼び出し先
class bb {
protected: string TestEventFunc(int a, int b) {
return boost::io::str(boost::format("a+b=%d") % a+b);
}
}
//
int main(int, char**) {
aa AA;
bb BB;
AA.SetTestEvent( boost::bind(&bb::TestEventFunc, &BB, _1, _2);
AA.Run();
return 0;
}