Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to use the static_cast of C++

Shulou Source: shulou.com Published: 2022-06-01 19:24:27 09月16日 Update

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!

Tags: Compiling clocking typing error learning pointing examples objects pointers more problems static help checking no cheap practical don't do bad things even if it's inconspicuous next not to mention Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft MariaDB Linux Shulou Information Redmi