type casting problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
1  struct Foo
2  {
3  };
4 
5  struct Bar
6  {
7  };
8 
9 
10 int main()
11 {
12     Foo* f = new Foo;
13 
14     Bar* b1 = f;
15     Bar* b2 = static_cast<Bar*>(f);
16     Bar* b3 = dynamic_cast<Bar*>(f);
17     Bar* b4 = reinterpret_cast<Bar*>(f);
18     Bar* b5 = const_cast<Bar*>(f);
19 
20     return 0;
21 }

this is a question from some random site, asking which lines won't compile. My answer is 14, 16, 18. but the answer is also include 15. I did run this example in VS2010, it did give some compilation error on line 15.

"error C2440: 'static_cast' : cannot convert from 'Foo *' to 'Bar *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast"

anyone could explain why?
Thanks!!
Types pointed to are unrelated


It tells you why.
Topic archived. No new replies allowed.