开发者

What is dynamic type of object

开发者 https://www.devze.com 2023-04-10 07:51 出处:网络
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

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." Ref开发者_StackOverflowerences (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;
}


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


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.


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:

  • this may be Base
  • this may be Derived
  • this may be another derived class from Base that we know not yet

Therefore, in the following example:

int main(int argc, char* argv[]) {
  if (argc == 1) {
    Base base;
    print();
  } else {
    Derived derived;
    print(derived);
  }
};
  • The static and dynamic type of base is Base and derived is Derived.
  • In the print method, the static type of b is Base (always)
  • Depending on the number of arguments, the 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.


Dynamic memory allocation is always done at run time.it can be achieved using "new" keyword. but there is one more case as mentioned in your problem *p=&d here as you have made base class function "Virtual" it tells compiler to treat "p" by it Content and not by the type of pointer to which it belong.so this is one of the Dynamic memory allocation as compiler never know which class object's address your going to store at run time ,it only knows which type of pointer it is (i.e. Base class pointer or derived class pointer).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号