久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何從 C++ 對象中獲取類名?

How can I get the class name from a C++ object?(如何從 C++ 對象中獲取類名?)
本文介紹了如何從 C++ 對象中獲取類名?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

是否也可以獲取對象名稱?

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

C++ 是否提供任何機制來實現這一點?

推薦答案

您可以使用預處理器來顯示變量的名稱.例如

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()<<"	"<< quote(A) <<"
";
    return 0;
}

輸出

3one    A

在我的機器上.# 將一個token變成一個字符串,經過預處理后的行是

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

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

當然如果你做類似的事情

Of course if you do something like

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

你會得到

3one B

因為編譯器不會跟蹤所有變量的名稱.

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

在 gcc 中,typeid().name() 的結果是經過修改的類名,以獲得 破解版 使用

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<<"	"<< quote(A) <<"
";
    free(demangled);
    return 0;
}

這給了我

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

其他編譯器可能使用不同的命名方案.

Other compilers may use different naming schemes.

這篇關于如何從 C++ 對象中獲取類名?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How can I read and manipulate CSV file data in C++?(如何在 C++ 中讀取和操作 CSV 文件數據?)
In C++ why can#39;t I write a for() loop like this: for( int i = 1, double i2 = 0; (在 C++ 中,為什么我不能像這樣編寫 for() 循環: for( int i = 1, double i2 = 0;)
How does OpenMP handle nested loops?(OpenMP 如何處理嵌套循環?)
Reusing thread in loop c++(在循環 C++ 中重用線程)
Precise thread sleep needed. Max 1ms error(需要精確的線程睡眠.最大 1ms 誤差)
Is there ever a need for a quot;do {...} while ( )quot; loop?(是否需要“do {...} while ()?環形?)
主站蜘蛛池模板: 国产探花在线精品一区二区 | 国产精品视频中文字幕 | 91精品久久久久久久久中文字幕 | 欧美一级黑人aaaaaaa做受 | 日日夜夜免费精品 | 天天操天天插天天干 | 国产福利在线视频 | 欧美久久久久久久久中文字幕 | 国产精品伦理一区 | 亚洲激情在线观看 | 国产精品乱码一区二区三区 | 久久久精品 | 午夜爽爽爽男女免费观看影院 | 亚洲精品久久久久久国产精华液 | 色婷婷国产精品综合在线观看 | 久久久久91| 在线观看特色大片免费网站 | 欧美日韩国产精品一区 | 91久久| 国产在线高清 | 亚州成人 | 91av免费版| 91欧美精品成人综合在线观看 | 成人动漫视频网站 | 久久精品小短片 | 中文字幕精品一区二区三区精品 | 日韩三级免费网站 | 亚洲视频在线看 | 欧美一区二区视频 | 91精品国产91| 国产免费又色又爽又黄在线观看 | 国产色| 人人干人人艹 | 亚洲国产精品久久久 | 男女网站在线观看 | 欧美在线观看免费观看视频 | 国产乱码精品一品二品 | 色综合久久88色综合天天 | 亚洲国产成人精品久久 | 久久福利电影 | 三级黄色片在线 |