The main advantage of C ++ style styles, as you already mentioned, is type safety. Each action in C ++ processes one specific type of transformation (or a family of related transformations), and so the compiler can go and check that you are not doing the unnecessary transformations that you intended, or the sequence of transformations that are basically unsafe.
One thing to think about is that, although you are comfortable using C-style casts, and even though you haven't made any mistakes with them, other code-based people may not be as light with these scum as you. Using casting operators makes code more self-documenting. If you have something in C style, someone who reads the code may not immediately establish what exactly you are doing. If they see something like
T* ptr = (T*) var;
They may not immediately determine if it is
- Cast from base class to derived class or vice versa.
- Dropping
const discarded var - Converting from an integral type to a pointer.
While they can probably get this out of context, it’s much more obvious what happens if you use a listing like
T* ptr = static_cast<T*>(var);
or
T* ptr = const_cast<T*>(var);
Another reason to prefer C ++-style casting operators is that they make the code more resilient to change. For example, suppose I have this function:
void DoSomething(Base* ptr) { Derived* derived = (Derived *) ptr; DoSomethingElse(derived); }
Now suppose that I understand that this function should not make any changes to its argument, so I decided to note its const . For example:
void DoSomething(const Base* ptr) { Derived* derived = (Derived *) ptr; DoSomethingElse(derived); }
But now we have a problem - my C-style style, which was used only for down down, now also removes const ness. This can lead to a slight error when DoSomethingElse mutates the pointer that I pass, although the DoSomething promises method itself does not do this. If instead I wrote this code as
void DoSomething(Base* ptr) { Derived* derived = static_cast<Derived *>(ptr); DoSomethingElse(derived); }
And then change the code by adding const :
void DoSomething(const Base* ptr) { Derived* derived = static_cast<Derived *>(ptr); DoSomethingElse(derived); }
Now I will get a compiler error telling me that my old throw is broken, which may make me find that there is a logical error in the code (namely, that DoSomethingElse mutates its argument, so I cann’t naively make a ptr pointer to const .
In short, using C ++ casting operators makes the code more readable and more convenient. This makes the code logic more explicit. And this makes the code less error prone, as compiler errors catch either as they are created, or later when you return and change the old code. I would recommend trying to use C ++-style casting operators in the future for these main reasons.
As for whether you need to go back and try to replace your current C-style styles with C ++ - styles, it really is up to you. As an exercise, I would suggest doing this in order to learn how to study what types of techniques you use. In addition, you can find there a logical error that will make the search suitable for you!