c++user-defined-typestypeid

how typeid(type).name() decide name for user define class? and can i change this behaviour?


i want to know for user define class how typeid(type) will decide the name for type of user define class check my below code for student class i got an output like 7Student but i don't understand why 7 is appended before Student class.

#include <iostream>

class Person {
    protected:
        std::string name;

    public:
        Person(std::string name)
            : name(name) {}
};

class Student : public Person{
    private:
        std::string id;

    public:
        Student(std::string name,std::string id)
            : id(id) , Person(name) {}
};

template<typename Type>
class Test {
    private:
        Type type;

    public:
        Test(Type type)
            : type(type) {}

        const char* getType() const {
            return typeid(this->type).name();
        }
};

int main() {
    Test<int> *test1 = new Test<int>(5);
    std::cout<<test1->getType()<<std::endl;

    Test<float> *test2 = new Test<float>(1.1);
    std::cout<<test2->getType()<<std::endl;

    Test<double> *test3 = new Test<double>(1.1);
    std::cout<<test3->getType()<<std::endl;

    Test<long> *test4 = new Test<long>(11);
    std::cout<<test4->getType()<<std::endl;

    Test<unsigned int> *test5 = new Test<unsigned int>(11);
    std::cout<<test5->getType()<<std::endl;

    Test<Student> *test6 = new Test<Student>(*(new Student("visrut","111")));
    std::cout<<test6->getType()<<std::endl;      // 7Student

    Test<Person> *test7 = new Test<Person>(*(new Person("visrut")));
    std::cout<<test7->getType()<<std::endl;

    Test<std::string> *test8 = new Test<std::string>("visrut");
    std::cout<<test8->getType()<<std::endl;

    return 0;
}

i tried this code with extend Person class and without extend Person class but output is same for last type and it is 7Student.

for your reference on my g++ compiler output is below

i
f
d
l
j
7Student
6Person
NSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

Now when i tested for Person class also it's output is 6Person so i found this behaviour as {length of user-define class}{class-name} but i got weird output for std::string so i want to ask can i change this behaviour in Person or Student class is there is something built in method i can write in Person or Student class that return specific const char * for typeid().name() ?


Solution

  • The name() member of std::type_info is totally implementation defined. You cannot change what is output, and it is not known what will be output. It may even be different between runs.

    Returns an implementation defined null-terminated character string containing the name of the type. No guarantees are given; in particular, the returned string can be identical for several types and change between invocations of the same program.

    From here.

    If you need to define and retrieve a specific name from a class, you are looking at doing something called reflection (or a very simply form of it). This is not something that c++ was designed for and you should avoid relying on needing to do this if you can. IF you find you really need to do it, you can find more info at How can I add reflection to a C++ application?.