Reputation: 3658
What i think is that dynamic type means dynamically allocated object using new
. In the following case, do you say p
points to dynamic type or static type of object? In standard, it doesn't say about dynamic type being dynamic object.
1.3.3 - The type of the most derived object (1.8) to which the lvalue denoted by an lvalue expression refers. [Example: if a pointer (8.3.1) p whose static type is "pointer to class B" is pointing to an object of class D, derived from B (clause 10), the dynamic type of the expression *p is "D." References (8.3.2) are treated similarly. ]
Also what does it the following quote mean
The dynamic type of an rvalue expression is its static type
class Base {
virtual void foo(){}
};
class Derived : public Base {
void foo(){}
};
int main()
{
Derived d;
Base *p = &d;
}
Upvotes: 15
Views: 8772
Reputation: 69742
What i think is that dynamic type means dynamically allocated object using new.
Nope.
The dynamic type is the real type of an object that might be accessed via a reference (pointer included) that point to a base type of its real type.
That is, if we have :
class A {
};
class B : public A { };
B l;
A& k = l;
Here k is a reference to an object of type A, but the real type of the referred object, its dynamic type, is B.
Here "dynamic" has the meaning of "known only at run-time".
Upvotes: 26
Reputation: 299999
In a statically typed language, such as C++ or Java for example, static
may refer to the information known at compilation time while dynamic
refers to the information known at runtime.
For example:
struct Base { virtual std::string name() const { return "Base"; } };
struct Derived: Base { std::string name() const { return "Derived"; } };
void print(Base const& b) { std::cout << b.name() << "\n"; }
In the print
method, the static
type of b
is Base const&
. Therefore, the compiler will check that all methods called exist in the context of a Base
object.
However, when execution comes, the call to name
, as the method is virtual, is performed with regard to the dynamic
type of the object:
Base
Derived
Base
that we know not yetTherefore, in the following example:
int main(int argc, char* argv[]) {
if (argc == 1) {
Base base;
print();
} else {
Derived derived;
print(derived);
}
};
static
and dynamic
type of base
is Base
and derived
is Derived
.print
method, the static
type of b
is Base
(always)dynamic
of b
is either Base
or Derived
It is a current mistake to assume that polymorphism is necessarily based on dynamic memory allocation, but the two concepts, while not orthogonal, can be used without one another in some conditions.
Upvotes: 5
Reputation: 22020
The static type is the type of the variable, which is the only type known at compile time (hence considered static - cannot change). The dynamic type is the type of the object that is actually being pointed at run-time. Dynamic here means it's only known at run time, which means it might change (namely one variable can point on various objects of various types).
The use of new
in this content is not relevant, as your own example shows. In your main, the static and dynamic type of d
is Derived
, because it's not a pointer or reference. p
, however, has a static type of Base
, but in your code, the dynamic type would be Derived
.
Upvotes: 5