如何从C ++对象获取类名? [英] How can I get the class name from a C++ object?

查看:1056
本文介绍了如何从C ++对象获取类名?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否也可以获取对象名称?

 #include< cstdio> 

class one {
public:
int no_of_students;
one(){no_of_students = 0; }
void new_admission(){no_of_students ++; }
};

int main(){
one A;
for(int i = 0; i <99; i ++){
A.new_admission();
}
cout <<class<< [classname]<<<< [objectname]<<has
< ; A.no_of_students<<students;
}

其中我可以获取名称,例如

  [classname] = A.classname()= one 
[objectname] = A.objectname()= A



C ++是否提供了任何机制来实现这一点?

解决方案

可以使用预处理器显示变量的名称。例如

  #include< iostream> 
#define quote(x)#x
class one {};
int main(){
one A;
std :: cout<< typeid(A).name()<<\t< quote(A)<<\\\
;
return 0;
}

输出

  3one A 

在预处理行之后将令牌更改为字符串

  std :: cout<< typeid(A).name()<<\t< A<\ n; 

当然,如果你做了

  void foo(one B){
std :: cout<< typeid(B).name()<<\t< quote(B)<<\\\
;
}
int main(){
one A;
foo(A);
return 0;
}

您将获得

  3one B 



正如在gcc中发生的一样,typeid()的结果name()是被改变的类名,得到 demangled version use

  #include< iostream> 
#include< cxxabi.h>
#define quote(x)#x
template< typename foo,typename bar> class one {};
int main(){
one< int,one< double,int& >一个;
int status;
char * demangled = abi :: __ cxa_demangle(typeid(A).name(),0,0,& status);
std :: cout<< demangled<<\t<< quote(A)<<\\\
;
免费(demangled);
return 0;
}

  one< int,one< double,int> > A 

其他编译器可能使用不同的命名方案。


Is it possible to get the object name too?

#include<cstdio>

class one {
public:
    int no_of_students;
    one() { no_of_students = 0; }
    void new_admission() { no_of_students++; }
};

int main() {
    one A;
    for(int i = 0; i < 99; i++) {
        A.new_admission();
    }
    cout<<"class"<<[classname]<<" "<<[objectname]<<"has "
        <<A.no_of_students<<" students";
}

where I can fetch the names, something like

[classname] = A.classname() = one
[objectname] = A.objectname() = A

Does C++ provide any mechanism to achieve this?

解决方案

You can display the name of a variable by using the preprocessor. For instance

#include <iostream>
#define quote(x) #x
class one {};
int main(){
    one A;
    std::cout<<typeid(A).name()<<"\t"<< quote(A) <<"\n";
    return 0;
}

outputs

3one    A

on my machine. The # changes a token into a string, after preprocessing the line is

std::cout<<typeid(A).name()<<"\t"<< "A" <<"\n";

Of course if you do something like

void foo(one B){
    std::cout<<typeid(B).name()<<"\t"<< quote(B) <<"\n";
}
int main(){
    one A;
    foo(A);
    return 0;
}

you will get

3one B

as the compiler doesn't keep track of all of the variable's names.

As it happens in gcc the result of typeid().name() is the mangled class name, to get the demangled version use

#include <iostream>
#include <cxxabi.h>
#define quote(x) #x
template <typename foo,typename bar> class one{ };
int main(){
    one<int,one<double, int> > A;
    int status;
    char * demangled = abi::__cxa_demangle(typeid(A).name(),0,0,&status);
    std::cout<<demangled<<"\t"<< quote(A) <<"\n";
    free(demangled);
    return 0;
}

which gives me

one<int, one<double, int> > A

Other compilers may use different naming schemes.

这篇关于如何从C ++对象获取类名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆