What is dynamic type of object

馋奶兔 提交于 2019-12-18 02:51:37

问题


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;
}

回答1:


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




回答2:


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.




回答3:


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.




回答4:


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



来源:https://stackoverflow.com/questions/7649649/what-is-dynamic-type-of-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!