C++. typeid로 개체 형식 알아보기

최대 1 분 소요

🌟 typeid

typeid는 typeinfo가 필요하며, 원하는 개체의 자료형 타입을 알 수 있다.

typeid(개체 이름).name()

반환은 const char*로 반환함.

🌟 사용법

#include <iostream>
#include <typeinfo>
using namespace std;

int main()
{
    int i = 0;
    char c = 'a';
    
    cout << typeid(i).name() << endl;
    cout << typeid(c).name() << endl;
}

이렇게 하면 intchar을 출력할 것이다.

끝!

댓글남기기