What does mean Cast<>(); function do?

Hi…

I would like to know what acutally this does… Can somebody clearly explain it?
Thank you…

There is some good information here: http://en.cppreference.com/w/cpp/language/explicit_cast

When you are working with a parent class but need access to child member functions and variables, casting allows you to treat the parent class as if it were the casted class.

Imagine if you were blindfolded and led into a room. Knowing that it’s a room, you can safely assume that it’s got a ceiling, a floor, and walls all around, but not necessarily anything else. As far as you know, there’s nothing to do but walk around. Now imagine you were told that it’s actually a kitchen. Now you can safely assume that you’ve got a stove, a refrigerator, and a sink, which means you can cook a meal or grab yourself a drink. The room itself did not change at all, only your perception of it. The room was essentially “casted” into a kitchen.

This is probably a poor analogy, but hopefully you understand that when you cast an object into something else, you are not actually changing it at all, but merely changing the user’s “perception” of it and what it can do.

6 Likes

Brilliant…
Perfectly explained !!!

again
Thanks Ryan

Privacy & Terms