Home c++ Why do you need Dynamic_Cast?

Why do you need Dynamic_Cast?




What do you need Dynamic_cast ? What advantages is entry 1 compared with the 2 record and what flaws? In what cases is the option to use?

Answer 1, Authority 100%

Actually, in the option, as you recorded, you do not need to give anything, because the object of the derived class is already the object of the base. So you can just write

a * a = b;

But if on the contrary –

b * b = a;

Problems begin. Because usually talks about bad design . You want to use the descendant where only ancestor is used. Those. In essence, add some kind of tricky behavior there, where nothing is known about him and should not be known.

Dynamic_Cast Allows you not to wrap the entire program because you have done something wrong. Well, for example,

void func (base * b)
  ((DERIED *) B) - & gt; derivedFunc ();

Here you call a member function that has Derived , but which is not in Base . But what will happen if you really pass the pointer to the base function? Pointer to other descendant base who does not know anything about this feature?

and Dynamic_Cast allows you to at least make sure that it is exactly what you need, i.e. What is there really pointer on derived .

What is before casting in the style of C – (A *) b , then, perhaps, the closest reinterpret_cast is to simply consider bits as having a different type. Without any checks. What is already dangerous, but at least in the text of the program in the eye this long word will be rushed – reinterpret_cast – as a signpost of danger. And this is not a joke, I repeat strawstrups – about the long clumsy names __ Cast .

p.s. I do not need to remind that when using Dynamic_Cast you need virtual functions, and explain why?

Answer 2, Authority 40%

There is no reason to use type 2. Recording. It appeared as ensuring compatibility with the type of types from C and old versions of C++.

According to the current standards you need to use dynamic_cast.
If you want to understand what different Cast’ov options are different – let me know. But in this case, everything is trivial. We give an object of a class-descede to the basic class

Additionally, pay attention to what the Casts are good that they basically fit into the exemption processing system and give human error codes. And the use of C-STYLE bringing is the easiest (and not the best !!!) way to shut the compiler. And disguise the error, and then heroically try to find it and / or correct

Answer 3, Authority 15%

dyanmic_cast is needed to safely perform a downgrade (on the type of basic class to the type of heir). For example

Calss Base {
  Virtual ~ Base () {}
Calss A: Public Base {};
Calss B: Public Base {};
Void Foo (A * A) {
  Base * Base = A;
  // 1 is compiled normally.
  // Bringing the type will be executed, but at the execution stage
  // It turns out some kind of nonsense, the reason of which will
  // find oh how difficult
  B * b1 = (b *) base;
  // 2 Fully identical 1.
  // just such a record is easier to find with Grep
  // or similar means. Also static_cast not
  // removes Const and Voltile modifiers that in
  // some cases will avoid errors
  B * b2 = static_cast & lt; b * & gt; (base);
  // 3 as well as cases 1 and 2 successfully compiled.
  // But B3 will be equal to 0. Dynamic_cast using RTTI
  // determined that an attempt to bring to
  // incorrect type, and returned 0.
  B * b3 = dynamic_cast & lt; b * & gt; (base);
  // Now we can write
  if (B3 == 0) {
    STD :: CERR & LT; & LT; "Something went wrong \ n"; 
   // and quickly locate the error

also so that you can use Dynamic_cast In the basic class, there must be at least one virtual function (the destructor is suitable).

If used instead of reference pointers, then dyamic_cast will throw out the std :: bad_cast if it is wrong to drive

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions