久久久久久久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天堂| 中文在线a在线 | 国产欧美精品一区二区色综合 | 自拍 亚洲 欧美 老师 丝袜 | 亚洲一区二区三区在线播放 | 欧美视频免费在线 | 亚洲va在线va天堂va狼色在线 | 日韩中文字幕av | 在线成人免费观看 | 日本视频一区二区 | 亚洲一区国产精品 | 五月婷婷婷 | 91麻豆精品国产91久久久更新资源速度超快 | 亚洲男人网 | 精品九九 | 亚洲精品一区二区三区蜜桃久 | 高清视频一区二区三区 | 日日噜噜噜夜夜爽爽狠狠视频, | 国产一二区视频 | 精品一区二区三区四区五区 | 精品1区 | 久久久久久国产 | 欧美久久天堂 | 亚洲成人福利 | 久久久久成人精品 | 国产小视频在线 | 性欧美精品一区二区三区在线播放 | 国产精品大片 | 久久成人精品视频 | 国产成人午夜精品影院游乐网 | 国产午夜精品一区二区三区嫩草 | 日韩视频―中文字幕 | 久久网站黄 | 国产精品国产 | 亚洲精品视频在线观看免费 | 99久久婷婷国产综合精品电影 | 久久精品91 |