In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to use C++ 's static_cast". In daily operation, I believe many people have doubts about how to use C++ 's static_cast. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how to use C++ 's static_cast". Next, please follow the editor to study!
Suppose you have the following inheritance relationship.
Class Animal
{
}
Class Dog: public Animal
{
}
Class Cat: public Animal
{
}
Let's start with a simple preparation.
Dog* dog = new Dog ()
Animal* animal = dog
Assigning a pointer to a derived class type to a base class type pointer does not require type conversion because:
Dog is an Animal
By the same token, because the above judgment is not valid, the following transformation requires the use of static_cast.
Cat* cat1 = static_cast (dog); / / example 1 Magi NG
This line will have compilation errors, and of course the reason should be easy to understand, because of Dog is not a Cat. Let's take a look at the next example.
Dog* sdog = static_cast (animal); / / example 2 is OK
You might say there's no problem, because animal points to Dog.
But look at the next example.
Cat* cat2 = static_cast (animal); / / case 3.
Although animal points to a Dog object, it can be compiled.
What's wrong?
Example 2 is misunderstood. What the animal actually points to will not be known until the program is executed (dynamically). Static_cast will only check statically, that is,
Is it possible to convert Dog to Cat? No, compilation error.
Is it possible to convert Animal to Dog? It is possible to compile through
Is it possible to convert Animal to Cat? It is possible to compile through
Conclusion: staitc_cast can statically check the conversion and help us find out some errors.
The ancients said: don't do bad things even if it's inconspicuous, don't give up doing good even if it's not important.
Of course, the sooner you find a mistake, the better, not to mention being automatically in the cheap stage.
It would be better if you don't have to cast. If you can't, start by using static_case/dynamic_cast/const_cast/reinterpret_cast correctly.
At this point, the study on "how to use C++ 's static_cast" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.