Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is dynamic type of object

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;
}
like image 614
user103214 Avatar asked Oct 04 '11 14:10

user103214


1 Answers

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".

like image 154
Klaim Avatar answered Oct 12 '22 01:10

Klaim