什么是对象的动态类型 [英] What is dynamic type of object

查看:255
本文介绍了什么是对象的动态类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为动态类型意味着使用 new 动态分配对象。在下面的情况下,你说 p 指向动态类型还是静态类型的对象?在标准中,它不说动态类型是动态对象。


1.3.3 - 最导出的对象的类型),通过一个左值表达式表示
的左值。 [示例:如果其
静态类型是指向类B的指针的指针(8.3.1)p指向从B(子句10)导出的类
D的对象,则动态类型的表达式* p
是D.参考文献(8.3.2)同样对待。 ]


此外,以下引用是什么意思


右值表达式的动态类型是其静态类型




  class Base { 
virtual void foo(){}
};

类派生:public Base {
void foo(){}
};

int main()
{
Derived d;
Base * p =& d;
}


解决方案


我认为动态类型意味着动态分配对象
使用new。



$ b

动态类型是一个对象的真实类型,它可以通过指向它的实际类型的基本类型的引用(包含指针)访问。



也就是说,如果我们有:

  class A {

}

class B:public A {};


B l;
A& k = 1;

这里k是对A类型对象的引用,但是引用对象的实际类型,它的动态类型是B.



此处动态的含义是仅在运行时知道。

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

解决方案

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

这篇关于什么是对象的动态类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆