S
S
sddvxd2018-08-21 21:50:27
C++ / C#
sddvxd, 2018-08-21 21:50:27

What is the difference between dynamic type checking and static type checking?

It was necessary to translate the link type from one to another. Found 2 options - static_cast and dynamic_cast. The first one says that it is unsafe because types are checked at compile time. Here is the question - why did they separate type checking into dynamic and static?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
devalone, 2018-08-22
@sddvxd

I may not have asked the right question. I would like to know why the compiler cannot check everything at compile time. why else add dynamic checking?

class Object {
public:
virtual ~Object() {}
};
class Weapon : public Object {
public:
virtual ~Weapon() {}
};
class Player : public Object {
public:
virtual ~Player() {}
};

void handleObject(Object* object) {
  // What is object? Is it weapon or player?
};

// ....

Object* object = rand() % 100 > 50 ? new Weapon() : new Player();
handleObject(object);

V
vreitech, 2018-08-21
@fzfx

because each type of check has its own advantages.
static cast is faster.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question