Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static vs dynamic type checking in C++

Tags:

c++

I want to know what are static and dynamic type checking and the differences between them.

like image 668
rkb Avatar asked Aug 28 '09 15:08

rkb


People also ask

What is the difference between static and dynamic type checking?

The key difference between the two is that with static type checking, the type of variable is known at compile time (it checks the type of variable before running) while with dynamic type checking, the type of variable is known at runtime (it checks the type of variable while executing).

Does C have static type checking?

Programming with a static type system often requires more design and implementation effort. Languages like Pascal and C have static type checking. Type checking is used to check the correctness of the program before its execution.

Is C statically or dynamically-typed?

Statically-typed: C, C++, Java. Dynamically-typed: Perl, Ruby, Python, PHP, JavaScript.

What is the difference between dynamic and static programming?

A dynamic language (Lisp, Perl, Python, Ruby) is designed to optimize programmer efficiency, so you can implement functionality with less code. A static language (C, C++, etc) is designed to optimize hardware efficiency, so that the code you write executes as quickly as possible.


1 Answers

Static type checking means that type checking occurs at compile time. No type information is used at runtime in that case.

Dynamic type checking occurs when type information is used at runtime. C++ uses a mechanism called RTTI (runtime type information) to implement this. The most common example where RTTI is used is the dynamic_cast operator which allows downcasting of polymorphic types:

// assuming that Circle derives from Shape...
Shape *shape = new Circle(50);
Circle *circle = dynamic_cast<Circle*> shape;

Furthermore, you can use the typeid operator to find out about the runtime type of objects. For example, you can use it to check whether the shape in the example is a circle or a rectangle. Here is some further information.

like image 115
VoidPointer Avatar answered Oct 08 '22 19:10

VoidPointer