C ++ typeid
과 typeof
C ++ 의 차이점이 무엇인지 궁금합니다 . 내가 아는 것은 다음과 같습니다.
typeof
C 및 C ++ Boost 라이브러리 의 GCC 확장에 정의되어 있습니다.
또한, 내가 찾은 곳에서 내가 만든 typeid
예상 코드를 반환하지 않는 테스트 코드 테스트가 있습니다. 왜?
main.cpp
#include <iostream>
#include <typeinfo> //for 'typeid' to work
class Person {
public:
// ... Person members ...
virtual ~Person() {}
};
class Employee : public Person {
// ... Employee members ...
};
int main () {
Person person;
Employee employee;
Person *ptr = &employee;
int t = 3;
std::cout << typeid(t).name() << std::endl;
std::cout << typeid(person).name() << std::endl; // Person (statically known at compile-time)
std::cout << typeid(employee).name() << std::endl; // Employee (statically known at compile-time)
std::cout << typeid(ptr).name() << std::endl; // Person * (statically known at compile-time)
std::cout << typeid(*ptr).name() << std::endl; // Employee (looked up dynamically at run-time
// because it is the dereference of a pointer
// to a polymorphic class)
}
산출:
bash-3.2$ g++ -Wall main.cpp -o main
bash-3.2$ ./main
i
6Person
8Employee
P6Person
8Employee
name()
은 구현 정의입니다. 그것은 올바른 C ++ 식별자 이름, 다만 필요는 없습니다 뭔가 독특한 유형을 식별합니다. 구현에서 컴파일러의 일반 이름 관리 체계를 사용하는 것 같습니다.