차이점은 무엇입니까 typeid
과 typeof
C++에서 궁금합니다. 여기에 내가 무엇을 알고 :'typeid'대 'typeof'는 C++에서
typeof
은 C의 GCC 확장과 C++ Boost 라이브러리에서 정의됩니다.
또한, 여기에 내가 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++ 식별자 이름 일 필요는 없습니다. 유형을 고유하게 식별하는 * something * 일뿐입니다. 당신의 구현이 컴파일러의 일반적인 이름 - mangling scheme을 사용하는 것처럼 보입니다. –
감사합니다. Rob! 나는 en.wikipedia.org/wiki/Typeid에서 본 것과 똑같은 타입의 이름을 기대하고있었습니다. 여기서 이름 짓기는 무엇입니까? – Tim