Originally Posted by
Davros
OK. I follow so far, but this is where I start to get all confused...
Under what circumstances does reinterpret cast fail, and how will it fail. For example, is a runtime check performed to determine that the value stored in an integer is a valid WNDPROC pointer?
It can fail (compile time) if the you are doing something silly like casting a whole object to an int. It can often issue warnigns for less troublesome size differences.
Originally Posted by
Davros
If so, would I be right in thinking it is like dynamic_cast except there is no requirement for a virtual base class. If so can I use it instead of a dynamic_cast? Will it fail where dynamic_cast fails?
No..dynamic cast will fail if the pointer is not a base ptr/ref to a derived pointer. It only really works for inheritance etc. You can use reinterpret_cast for this but if you are not carefull you will likely get a crash
Originally Posted by
Davros
If I should only use static_cast for things like ints to doubles, what's the advantage of using it over the following:
int a = 5;
double b = a;
It's a little more handy than that
Have a look at this
Code:
#include <iostream>
struct foobar;
void Edit(foobar* f);
struct foobar
{
int m_i;
foobar():m_i(64){}
};
void Edit(foobar* f)
{
std::cout << "Edited to - " << ++(f->m_i) << std::endl;
}
void PrintPointer(long PointerAsVal)
{
std::cout << "Address - " << PointerAsVal << std::endl;
}
int main (int argc, char **argv)
{
const foobar a;
std::cout << "Value as intr - " << a.m_i << std::endl;
std::cout << "Value as char - " << static_cast<char>(a.m_i) << std::endl;
Edit(const_cast<foobar*>(&a));
PrintPointer(reinterpret_cast<long>(&a));
}