how typeid works?

i'm a little bit curious, how the typeid works? i mean, like, the syntax of
typeid(variable).name();
is strange for me. can anybody explain it to me? and thus, it'll helps me to understand for any similar "cases"... :D
typeid returns a type_info object. See here: http://www.cplusplus.com/reference/std/typeinfo/type_info/
typeid operator is Run Time Type Information Operator(RTTI) in C++. It returns objects type during runtime. for example,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <typeinfo>

using namespace std;

class Animal{
    public:
        virtual void display(){}
};

class Cow:public Animal{};

class Dog:public Animal {};

int main(){
    Animal *anm;
    Cow cw;
    Dog dg;

    cout << "type of anm is : "<<typeid(anm).name()<<endl;
    cout << "type of cw is : "<<typeid(cw).name()<<endl;
    cout << "type of dg is : "<<typeid(dg ).name()<<endl;

    //after initializing base pointer with address of derived class object
    anm = &cw; //assigns anm with address of cw.since Animal is virtual, anm is now type of //Cow
    cout<<"type of  *anm when pointing to cw is: "<<typeid(*anm).name()<<endl;
    anm = &dg;
    cout<<"type of  *anm when pointing to dg is: "<<typeid(*anm).name()<<endl;
}


output

1
2
3
4
5
type of anm is : Animal
type of cw is : Cow
type of dg is : Dog
type of  *anm when pointing to cw is: Cow
type of  *anm when pointing to dg is: Dog


This example shows that , initially anm is type of Animal, cw is type of Cow and dg is type of Dog as expected. But when anm is initialized to address of cw, due to property of virtual function, anm is now type of Cow but not type of Animal . So typeid can be used to know the type of Object only at runtime. Compiler is unknown about it until runtime
Last edited on
i'm really sorry, my question is not clear at all... what i mean is, the syntax is seems like there's a function within a function.

 
typeid(variable).name()


so, i'm wondering does the structure is like this?

1
2
3
4
5
6
7
8
class typeid {
public:
	typeid(void* var) {
		char * name() {
			//...
		}
	}
}


or something similar, perhaps... sorry, my question probably sounds weird...
so... can anybody explain this to me?
so, i'm wondering does the structure is like this?

No. Again: http://www.cplusplus.com/reference/std/typeinfo/type_info/
Think of typeid as a function (it's actually an operator) returning a type_info object.
Last edited on
It's called Function Chaining. It's when you invoke a function from an object returned by the first function. For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
struct Simple
{
    void Print() const
    {
        std::cout << "Simple::Print()" << std::endl;
    }
};

Simple &NewInstance()
{
    // When I invoke this function, X will be returned
    // by reference, and not a copy.
    static Simple X;
    return(X);
}

int main()
{
    NewInstance().Print();

    // Here, Print() is a method of Simple. Since NewInstance()
    // returned X, an instance of Simple, by reference, I reached
    // into X and called its Print() method.
}


Wazzak
Last edited on
Topic archived. No new replies allowed.