Open Source Your Knowledge, Become a Contributor
Technology knowledge has to be shared and made accessible for free. Join the movement.
description:
Only reinterpret_cast can be used to convert a pointer to an object to a pointer to an unrelated object type. The dynamic cast would fail at run-time, however on most compilers it will also fail to compile because there are no virtual functions in the class of the pointer being casted.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
struct Foo
{
};
struct Bar
{
};
int main()
{
Foo* f = new Foo;
Bar* b1 = f;
Bar* b2 = static_cast<Bar*>(f);
Bar* b3 = dynamic_cast<Bar*>(f);
Bar* b4 = reinterpret_cast<Bar*>(f);
Bar* b5 = const_cast<Bar*>(f);
return 0;
}
Enter to Rename, Shift+Enter to Preview
Open Source Your Knowledge: become a Contributor and help others learn. Create New Content