C++ is static typed language, why can we get type at runtime
Asked Answered
L

3

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

For example we can get type like this. C++ is static typed language, why can we get type at runtime

Lexical answered 24/9, 2013 at 17:18 Comment(3)
Each object has a static type (which is known at compile-time) and a dynamic type (which is the type it actually has at run-time). The static and dynamic type commonly differ when dealing with class hierarchies.Isabelleisac
The short answer to "why" is: "because there are circumstances under which it's useful to do so."Aruspex
Did you mean "why can't we get type at runtime"?Anglin
S
5

Variables in C++ have a statically determined type. Objects don't necessarily. You only need to handle objects through variables of statically known type.

Examples:

int * p = new int[argc];   // there is an object whose dynamic type is int[argc]

Base * p = new Derived;    // *p has dynamic type Derived

Base * q = rand() % 2 ? new Derived1 : new Derived2;    // ???
Sardou answered 24/9, 2013 at 17:20 Comment(8)
For the record, I, for one, do not agree with this definition of "Object". Objects have static types, though expressions (such as *p) may not.Krafftebing
@BenjaminLindley In the spec it's expressions which have static types and objects which have dynamic types. A dynamic type is the type of the most derived object of an object referred to by an expression, and a static type is the type of an expression resulting from program analysis exclusive of execution semantics. see 1.3.7 and 1.3.23.Rhododendron
@bames53: I'm reading it, and the meaning you seem to be taking from it is not the meaning I take from it. 1.3.23 is saying that expressions have a static type (*p has a static type of Base&, always). 1.3.7 is saying that expressions also have a dynamic type (in this case, the dynamic type of *p is Derived&, but that may change), not that objects have a dynamic type.Krafftebing
@BenjaminLindley: new T[N] definitely creates an array-of-N-T, which is a type, and it's not statically determined, non?Sardou
In the sense "static type" is used here (i.e. the type is known at compile time), objects definitely do not have static type. There is another sense, however: an object never changes its type, once it is constructed, and in that sense, the type is static.Klink
@KerrekSB: I would argue, though not vehemently, that the object created with new T[N] when N is 5 is not the same object as the object which is created when N is 6. Therefore, there is no dynamically typed object. But the discussion would take too long, so I'll stop there. I mostly agree with the answer, I just think the wording is incorrect.Krafftebing
@BenjaminLindley Okay, yes 1.3.7 says a dynamic type is an attribute of a glvalue expression, and it is determined by looking at the object referenced and taking the type of its most derived object. As such it seems reasonable to say that an object has a dynamic type which is the type of its most derived object. This is similar to describing objects as having a static type when the spec only defines that term for expressions. Note that the standard uses both 'static type of the object' and 'dynamic type of the object' in many places.Rhododendron
@BenjaminLindley Also note that you didn't just say expression have static types, you said "Objects have static types, though expressions (such as *p) may not."Rhododendron
L
4

C++ is a static type language. It means you can not create a new class/type declaration in run-time and instance an object/variable of it, which is possible in Javascript or PHP.

dynamic_cast is part of RTTI, the C++ attempt to give information of types in run-time. When you cast an object via dynamic_cast, you aren't creating new type, you're just doing a polymophic thing.

However, you can say C++ is both static and dynamic type.

Lonnie answered 24/9, 2013 at 17:29 Comment(1)
C++ is statically typed in the sense that the type of a variable is declared at compile time. In the sense used here, this has nothing to do with the fact that you cannot create new types at runtime (although that is yet another reasonable meaning which one could give to the expression).Klink
A
4

Sine there seems to be some disagreement between a comment (saying objects have static types) and an answer (saying variables have static types, but objects don't necessarily), I guess I'll throw in my two cents worth on the subject.

In C++, both variables and objects have static types. When you create an object (e.g., whether global, local, or created with new) you have to give a static specification of its type.

Likewise, when you create a variable, it always has a static type. For example, T *x and Tprime &y define x as a pointer to T, and y as a reference to TPrime respectively.

The one place things get dynamic is that it's possible for a pointer or reference to refer not only to the static type for which it is defined, but also to any other type derived from that type. A variable of type "pointer to T" is really implicitly of type "pointer to T or any derivative of T" (and likewise with references).

Therefore, given a variable of type (pointer|reference) to T referring to some object, the pointer itself, and the object to which it refers both have static types -- but they aren't necessarily the same static type.

dynamic_cast allows you to determine the static type of the referred-to object, even when/if it is/might be different from the static type of the pointer/reference being used to refer to the object. dynamic_cast also allows you to determine an intermediate type that's not really the static type of the pointer or of the object to which it refers, if the inheritance hierarchy includes some intermediate type between the two. For example, given:

struct base {
    virtual ~base() {}
};

struct intermediate : base {};

struct derived : intermediate {};

...we could have something like:

base *b = new derived;
intermediate *i = dynamic_cast<intermediate *>(b);

...and the cast would succeed.

Aruspex answered 24/9, 2013 at 17:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.