[[PageNavi(NavigationList)]]
==== クラスのクラス名(フル名)を取得する ====
{{{ html
<span style="float:right">
}}}
{{{ GoogleAdsense
<script type="text/javascript"><!--
google_ad_client = "ca-pub-0702888637712330";
/* 20120131 */
google_ad_slot = "8641490082";
google_ad_width = 300;
google_ad_height = 250;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
}}}
{{{ html
</span>
}}}
以下の場合があるので、クラスのクラス名(フル名)が欲しいと思うかもしれません。
* log4cppで使用するカテゴリ名はクラス名のフル名の「::」の部分を「.」に置き換えたものなので、その文字列を#defineとかするのではなく、プログラム上で自動で取得するようにしたい。
* 例外(std::exception)が発生したときにメッセージ(what())だけでなく、例外の型名もログに出力したい。
===== やり方 =====
考え方としては、以下の通りです。
1. 型のtypeidを取得し、名前を取得
2. その名前をデマングルする
===== サンプル =====
サンプル(Linuxで確認しています):
{{{ code cpp
#include <stdio.h>
#include <stdlib.h>
#include <typeinfo>
#include <cxxabi.h>
namespace aaa{
namespace bbb{
class Ccc{
public:
Ccc(){}
virtual ~Ccc() {}
};
};
};
int main(void){
aaa::bbb::Ccc ccc;
const std::type_info& info = typeid(ccc);
int status;
char* name = abi::__cxa_demangle(info.name(),0,0,&status);
printf("%s\n",name);
free(name);
}
}}}
実行結果:
{{{
takashi@takashi-virtual-machine:~/デスクトップ/ctest5$ ./a.out
aaa::bbb::Ccc
}}}
[[PageNavi(NavigationList)]]