I don't often use typeid() (by "often" I mean "ever"), so I had to look this up. Here's a referral page:
http://cplusplus.com/reference/std/typeinfo/type_info/
Here's where your problem lies:
1 2
|
Circle *cp=0;
if(typeid(s)==typeid(cp))
|
There are two problems with this.
1)
s
is of type 'Shape*' and
cp
is of type 'Circle*', so this will always return false because the types are not the same. Therefore your casts are never happening.
2) 'cp' is a null pointer (doesn't point to anything) so you can't dereference it (if you try your program will crash).
What you're trying to do here is simple. In English, your desired if statement looks something like "If 's' points to a Circle". To translate that into more C++ friendly English, you could rephrase that as "If whatever 's' points to is the same type as a Circle". Which, in C++, looks like the following:
if(typeid(*s) == typeid(Circle))
Of course if you're using dynamic_cast you don't need typeid at all.
edit: to put that another way. What you're doing in your original post with typeid checks and static_cast is pretty much exactly what dynamic_cast does internally (automatically checks the types and only performs the cast if the type is correct).