a deffirence between "C" like type conversion and VC++ cast operators
Is there any difference between "C" like conversion and VC++ cast conversion operators (reinterpret_cast, dynamic_cast, static_cast).
I mean not a logical difference, I mean low-level code generation differences.
Maybe cast conversion operators are used just in help for programmers???
There is a big difference:
C style cast will just do it, assume that the casted object or variable is of a type in which it is casted.
If not, you programm can heavily crash.
C++ style includes additional information to each variable or object (you have to enable it in project options). It tests if the cast is possible. If not, it returns NULL. You can test result against NULL and assert.
With C++ style, your program is safer, but less efficient.
Is that mean that if the programmer watches for correctness of types conversions, then the program, which was written using "C" conversion style, can be more effieciently, than one, which was written using cast operators?
Originally posted by reptile
...the program, which was written using "C" conversion style, can be more effieciently, than one, which was written using cast operators?
Yes, that is the meaning.
If you use cast operators like dynamic_cast<>, then the compiler will test for you if the cast is correct but you will pay for this in reducing a performace.
On the other side, if you use C-style, and cast something wrong, that will be accepted from compiler, but you will have a garbage pointer at runtime. Your program is more efficent, but you have to take care that your pointers are valid, you can't test pointers against NULL.
If a reinterpret_cast returns NULL it means that there is some problem with your code. It will not crash but some functionality will not be available ( most likely you will leave the current execution block).
If you want to handle each cast this way, the chekings will propagate through your code and you'll have to insert a lot of otherwise unnecessary ifs. I'm not against defensive programming but I think there are better options ( programming by contract etc).
Of course you might want to use this to check some pointer's type. But that can also indicate some poor design/implementation.
In my opinion, reinterpret_cast should be used in DEBUG versions, followed by assertions.
I like it. I already thought about replacing C++ style casts manualy, once the program is tested.
I don't like to insert #ifdef _DEBUG somewhere in code, it makes code less readable, but, your way, it's only defined once as a macro.
Simple and strong.