How can I get the class name from a C++ object?

前端 未结 8 2169
名媛妹妹
名媛妹妹 2020-11-29 20:56

Is it possible to get the object name too?

#include

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


        
8条回答
  •  心在旅途
    2020-11-29 21:25

    An improvement for @Chubsdad answer,

    //main.cpp
    
    using namespace std;
    
    int main(){
    A a;
    a.run();
    }
    
    //A.h
    class A{
    public:
     A(){};
     void run();
    }
    
    //A.cpp
    #include 
    #include 
    void A::run(){
       cout << (string)typeid(this).name();
    }
    

    Which will print:

    class A*
    

提交回复
热议问题