16

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

3
  • 11
    The goal of stackoverflow is to become the repository of all programming knowledge, even if it exists somewhere else. Aug 28, 2009 at 23:02
  • 9
    I know its somewhere on google, but here you get multi definitions which helps you in understanding in a more clear way
    – rkb
    Aug 31, 2009 at 17:14
  • 1
    @jpinto3912 rkb is correct..you shouldn't stop people from asking such questions..you have no right to comment or object if it lies within goal of stack overflow... Nov 26, 2012 at 11:25

5 Answers 5

22

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.

1
  • 6
    It's worth noting that a static_cast is perfectly valid in the above, simple, example. dynamic_cast should only be used if you are unsure that shape is of type Circle, in which case dynamic_cast will return NULL (or throw an exception if you're casting a reference).
    – Dan O
    Aug 29, 2009 at 4:59
7

C++ has very little support for dynamic type checking. One way is through dynamic_cast and other is through type id. Both can be used only when RTTI support is enabled in compiler.

TYPE& dynamic_cast<TYPE&> (object);
TYPE* dynamic_cast<TYPE*> (object);

The dynamic_cast keyword casts a datum from one pointer or reference type to another, performing a runtime check to ensure the validity of the cast.

If you attempt to cast to pointer to a type that is not a type of actual object, the result of the cast will be NULL. If you attempt to cast to reference to a type that is not a type of actual object, the cast will throw a bad_cast exception.

Make sure there is at least one virtual function in Base class to make dynamic cast work.

// expre_typeid_Operator.cpp
// compile with: /GR /EHsc
#include <iostream>
#include <typeinfo.h>

class Base {
public:
   virtual void vvfunc() {}
};

class Derived : public Base {};

using namespace std;
int main() {
   Derived* pd = new Derived;
   Base* pb = pd;
   cout << typeid( pb ).name() << endl;   //prints "class Base *"
   cout << typeid( *pb ).name() << endl;   //prints "class Derived"
   cout << typeid( pd ).name() << endl;   //prints "class Derived *"
   cout << typeid( *pd ).name() << endl;   //prints "class Derived"
   delete pd;
}
1
  • you didn't address static type checking
    – csguy
    Dec 10, 2019 at 20:58
6

Assume you have:

class A {};
class B:A {};

A* a = new B();
B* b = new B();

For the static type, you look at how the variable is declared.

A* a = ...
B* b = ...

So the static type of a is A* (or to put it another way, the static type of *a is A).

And the static type of b is B* (or to put it another way, the static type of *b is B).

Note that a and b have a static type that is fixed by it's declaration - it doesn't matter what you put in them, they will keep the same static type. ("static" means "unchanging").


For the dynamic type, you look at what happens to be in the variable right now.

a = new B();
b = new B();

So the dynamic types of a and b are both B* (or to put it another way, the dynamic types of *a and *b are both B).

Note that the dynamic type can change - if you did a = new A() then they dynamic type of a just changed to A*. Sometimes you don't know what the dynamic type is - e.g. if you do a = somefunc() then a might have dynamic type A*, B* or even C* (if some code you haven't seen defines C as a subclass of A or B).

If A had a virtual method on it, then you could use dynamic_cast to figure out what the dynamic type is. (Typically, if you're using this sort of code, you want to be able to do delete a; for that to work A's destructor has to be virtual. And making A's destructor virtual is enough for dynamic_cast to work).

3

There are multiple types of casts available in C++.

The most common would be to use static_cast in order to cast a variable from one type of pointer to another. However, you can also use dynamic_cast, which will check to make sure (at runtime) that the pointers are of the correct type. With dynamic_cast, if the pointer is not of the right type, at runtime, it will return 0 instead.

// Assume these classes exist
// class A
// class B
// class C : B

C* c = new C();
A* a = new A();

B* b = static_cast<B*>(a); // this will work!
b = dynamic_cast<B*>(a); // b == NULL
b = dynamic_cast<B*>(c); // b is valid
2
  • "B* b = static_cast<B*>(a); // this will work!": Shouldn't you better comment this as "this will compile and you get a non-NULL pointer; But it will not necessarily work!"?
    – mmmmmmmm
    Aug 28, 2009 at 17:01
  • I think actually static_cast works only for related types and will refuse to compile, reinterpret_cast would be needed in this case. Those examples look all messed up to me. Aug 28, 2009 at 17:44
-1

Static type checking is type checking that is done at compile time. This is the only type of type checking that C++ does. Dynamic type checking is type checking done at run time. This is usually seen in dynamic interpreted languages, but is less common in compiled languages. Last I checked, C++ doesn't do any sort of dynamic type checking.

Edit: Apparently I'm out of date. See Reed's comment below.

1
  • 8
    dynamic_cast exists in C++, and does run time type checking on many C++ runtimes. Aug 28, 2009 at 15:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.