C++ typeid输出信息格式

C++ typeid输出信息格式

用这段程序做演示:

#include <iostream>
#include <typeinfo>

class Base { virtual void dummy() {} };
class Derived : public Base { /* ... */ };

int main() {
    Base* base_ptr = new Derived;

    // Using typeid to get the type of the object
	std::cout << "Type: " << typeid(*base_ptr).name() << '\n';
    std::cout << "Type: " << typeid(base_ptr).name() << '\n';

    delete base_ptr;
    return 0;
}

输出为:

Type: 7Derived
Type: P4Base

这里的数字应该是表示类型名的长度。

posted @ 2024-12-15 15:26  Gold_stein  阅读(32)  评论(0)    收藏  举报