Search code examples
c++typeid

What does "Z4mainE2" mean?


#include<iostream>
#include<typeinfo>
using namespace std;
int main(){
    class c1{
        public:
        int a ;

    };

c1 obj1;
cout<<typeid(obj1).name();  
}

I ran it on ideone and the typeid.name() returns Z4mainE2c1. It becomes apparent that c1 is the name of the class but what is Z4mainE2. Why is it not displaying the type name only?


Solution

  • Z4mainE2 is the result of "name mangling". Basically, C++ compilers are designed around a linker model which doesn't directly support things like function overloading, operators, or even class members. To support the various non-C features of C++, object code produced by the compiler adds special sequences to the generated names. While the mangled names aren't generally visible or important to the programmer, the typeinfo object on some platforms exposes them directly.