DEV Community

Cover image for C++ type casting with example for C developers

C++ type casting with example for C developers

Vishal Chovatiya on October 13, 2019

The typecasting is the feature which makes C++ more type-safe, robust & may convince you to use it over C. But this is also a more underrated t...
Collapse
 
alexandis profile image
alexandis

Most of explanations sound not very clear to me. For instance, regarding constness. It would be great to see more comments about given examples touching some basics for the people who, for instance, are trying to master C++ after C#, etc. I think it would be more efficient than addressing such people to other references, books...

Collapse
 
visheshpatel profile image
Vishal Chovatiya

isn't it subjective matter?

Collapse
 
alexandis profile image
alexandis

Every matter is subjective ;) We all express our thoughts and wishes.

Collapse
 
sehe profile image
sehe

Erm, (float)10/2 is not 2.5 coliru.stacked-crooked.com/a/77b57...

Also, "you can also write (float)10 like float(10) isn't correct and makes it seem as if they're interchangable. I know some later text mentions the danger (eg (float) "danger"s) but by then the damage might be done

Collapse
 
visheshpatel profile image
Vishal Chovatiya

Yes...! You are correct. But that is what my point is.
You shouldn't use C-style cast.

Collapse
 
delta456 profile image
Swastik Baranwal

Use new instead of malloc and delete instead of free.

There is malloc and free in C++ i.e. std::malloc and std::free respectively.

Collapse
 
visheshpatel profile image
Vishal Chovatiya

Hey man!
Thanks for pointing it out.

Collapse
 
delta456 profile image
Swastik Baranwal

You should update the following as well.

Thread Thread
 
visheshpatel profile image
Vishal Chovatiya

done

Collapse
 
somedood profile image
Basti Ortiz

Finally! This is exactly the article I have been looking for in the past year. Thank you for this!

Collapse
 
visheshpatel profile image
Vishal Chovatiya

Thanks, Dood...