忍者ブログ
趣味+メモ用のブログです。 GNU/Linux関連、OSS関連情報、調査事項になるでしょうが、何を書くか分かりません。
[84] [83] [82] [81] [80] [79] [78] [77] [76] [75] [74]
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

オブジェクトのクラス名参照


オブジェクトのクラス名の表示には、「typeid( xxx ).name()」を
使用すればよいが、マングルされた文字列が表示される。

デマングルの方法を書いてくださっているページがあったので、確認。

■ソース
 1 #include <typeinfo>
 2 #include <cxxabi.h> // for abi::__cxa_demangle
 3 #include <string>
 4 #include <iostream>
 5 
 6 
 7 /* デマングル関数 */
 8 char* demangle(const char *demangled)
 9 {
10   int status;
11   return abi::__cxa_demangle(demangled, 0, 0, &status);
12 }
13 
14 /* テスト用のクラス */
15 namespace Foo { namespace Bar {
16 class Baz1 {
17 };
18 class Baz2 {
19 };
20 
21 }; };
22 
23 
24 int main(void)
25 {
26   using namespace Foo::Bar;
27   using namespace std;
28   Baz1 baz1();
29   std::cout << "class name : " << demangle( typeid(baz1).name() ) << std::endl ;
30   int i;
31   std::cout << "int name : " << demangle( typeid(i).name() ) << std::endl ;
32 
33   return 0;
34 }
35 
■結果
$ g++ typeid.cpp
$ ./a.out
mangle   class name : FN3Foo3Bar4Baz1EvE
demangle class name : Foo::Bar::Baz1 ()
mangle     int name : i
demangle   int name : int

参考URL:http://0xcc.net/blog/archives/000095.html

PR


忍者ブログ [PR]
カレンダー
04 2024/05 06
S M T W T F S
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
フリーエリア
最新コメント
最新トラックバック
プロフィール
HN:
一乗寺 又兵衛
性別:
男性
職業:
コンパイル
趣味:
コンパイル
バーコード
ブログ内検索