Back
Close

Quiz 23

Anonymous
915 views

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.

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;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
JOIN US ON DISCORD
Online Participants