ブログ引っ越ししました~!!
http://toyo.sitepedia.jp/
C++で関数テーブルを作ってみました。
関数テーブルとテーブルに登録する関数の定義
関数テーブルの登録、テーブルに登録した関数の実行、テーブルに登録する関数の処理の実装
関数テーブルを使用する側の実装
実行した時の出力結果がこちら
大想定通りの結果だ、いつか仕事で使う場面が来るかなぁ
関数テーブルとテーブルに登録する関数の定義
#pragma once
#ifdef _DEBUG
# define MyOutputDebugString( str, ... ) \
{ \
TCHAR c[256]; \
_stprintf_s( c, str, __VA_ARGS__ ); \
OutputDebugString( c ); \
}
#else
# define MyOutputDebugString( str, ... ) // 空実装
#endif
// 関数テーブル管理クラス
class CTableMng
{
public:
// コンストラクタ
CTableMng(void);
// デストラクタ
~CTableMng(void);
// 関数テーブル
static int (CTableMng::*FuncTable[])(int);
// 関数テーブル実行処理
int ExecFunc(int iParam);
private:
// 関数テーブルに登録する関数その1
int Test1(int iParam);
// 関数テーブルに登録する関数その2
int Test2(int iParam);
// 関数テーブルに登録する関数その3
int Test3(int iParam);
};
関数テーブルの登録、テーブルに登録した関数の実行、テーブルに登録する関数の処理の実装#include "StdAfx.h"
#include "TableMng.h"
// 関数テーブル
// 関数を登録
int (CTableMng::*CTableMng::FuncTable[])(int) =
{
&CTableMng::Test1
, &CTableMng::Test2
, &CTableMng::Test3
, NULL
};
// コンストラクタ
CTableMng::CTableMng(void)
{
// 処理なし
}
// デストラクタ
CTableMng::~CTableMng(void)
{
// 処理なし
}
// 関数テーブル実行処理
int CTableMng::ExecFunc(int iParam)
{
if(NULL != FuncTable[iParam])
{
return (this->*FuncTable[iParam])(1);
}
return 0;
}
// 関数テーブルに登録する関数その1
int CTableMng::Test1(int iParam)
{
return iParam + 10;
}
// 関数テーブルに登録する関数その2
int CTableMng::Test2(int iParam)
{
return iParam + 20;
}
// 関数テーブルに登録する関数その3
int CTableMng::Test3(int iParam)
{
return iParam + 30;
}
関数テーブルを使用する側の実装// メイン処理
int _tmain(int argc, _TCHAR* argv[])
{
CTableMng cTblMng;
MyOutputDebugString( _T("%d\n"), cTblMng.ExecFunc(0) );
MyOutputDebugString( _T("%d\n"), cTblMng.ExecFunc(1) );
MyOutputDebugString( _T("%d\n"), cTblMng.ExecFunc(2) );
return 0;
}
実行した時の出力結果がこちら11 21 31
大想定通りの結果だ、いつか仕事で使う場面が来るかなぁ

AmebaブログでSyntaxHighlighterを使って
ソースコードを見やすくしてみた
いいんでないかなぁ
ソースコードを見やすくしてみた

いいんでないかなぁ

#ifdef __cplusplus
extern "C" {
#endif
// 【GetTestMessage】関数を公開APIとして、.NETからコールできるようにする
__declspec( dllexport ) const char* GetTestMessage();
#ifdef __cplusplus
}
#endif
const char Message[256] = "Test";
// 【公開API】
const char* GetTestMessage()
{
return Message;
}
[DllImport("TEST.dll")]
private static extern IntPtr GetMessage();
IntPtr pMsg = EIDGetScriptErrMessage();
String szMsg = Marshal.PtrToStringAnsi(pMsg);







