'typeid' versus 'typeof' in C++

后端 未结 6 1349
刺人心
刺人心 2020-11-28 18:08

I am wondering what the difference is between typeid and typeof in C++. Here\'s what I know:

  • typeid is mentioned in t

6条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-28 18:43

    typeid provides the type of the data at runtime, when asked for. Typedef is a compile time construct that defines a new type as stated after that. There is no typeof in C++ Output appears as (shown as inscribed comments):

    std::cout << typeid(t).name() << std::endl;  // i
    std::cout << typeid(person).name() << std::endl;   // 6Person
    std::cout << typeid(employee).name() << std::endl; // 8Employee
    std::cout << typeid(ptr).name() << std::endl;      // P6Person
    std::cout << typeid(*ptr).name() << std::endl;     //8Employee
    

提交回复
热议问题