How to get C++ object name in run time?

前端 未结 8 1136
失恋的感觉
失恋的感觉 2021-01-17 11:08

Can I get an object\'s name in run time (like getting an object\'s type via RTTI)? I want the object to be able to print its name.

8条回答
  •  有刺的猬
    2021-01-17 12:12

    This may be GCC-specific:

    #include 
    #include 
    
    template 
    void foo(T t)
    {
        std::cout << typeid(t).name() << std::endl;
    }
    

提交回复
热议问题