typeid为多态指针? [英] typeid for polymorphic pointers?

查看:193
本文介绍了typeid为多态指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白为什么指针不是多态类型,因为我们可以使用基类指针,指向派生类,来调用派生类的虚函数。这表明在运行时,系统可以确定指针是否是多态的,不是吗?

I don't understand why pointers aren't polymorphic types, since we can use base class pointers, which are pointing to derived classes, to call derived class' virtual function. This suggests at runtime, the system can determine if a pointer is polymorphic, doesn't it?

(这是一个后续问题,来自用于多态类型的typeid

(This is a follow-up question from typeid for polymorphic types)

推荐答案

您的问题有术语的不正确使用。 C ++语言在指针本身和这些指针指向的对象之间做了非常清楚的区分。指针类型不是多态的。指针本身没有多态性。真正可以是多态的是指针指向的类型。当指针指向多态类型时,我们经常[非正式地]调用多态指针(正如指向多态类型的指针的简写)。但是,当涉及到 typeid ,他们看到的东西非常正式。 typeid 指针类型从不是多态的。

Your question suffers from incorrect usage of terminology. C++ language makes a very clear distinction between pointers themselves and objects these pointers point to. Pointer types are not polymorphic. There's nothing polymorphic about the pointer itself. What can really be polymorphic is the type the pointer points to. When a pointer points to a polymorphic type, we often [informally] call it polymorphic pointer (just as a shorthand for "a pointer that points to a polymorphic type"). But when it comes to things like typeid, they see things very formally. For typeid pointer types are never polymorphic.

编译器不会确定指针是多态的运行。这种简单的区别总是在编译时立即知道。再次,如果指针被声明为指向多态类型的指针,则该指针被称为多态。多边形类型是包含虚拟函数(直接或间接)的类类型。显然,多态性的属性是类型的纯编译时属性。

And the compiler does not determine whether the pointer is polymorphic or not at run-time. This simple distinction is always immediately known at compile-time. Again, a pointer is referred to as polymorphic if it is declared as a pointer to polymorphic type. Polymorphic type is a class type that contains virtual functions (directly or indirectly). Obviously, the property of being polymorphic is a purely compile-time property of a type.

在这种情况下,在运行时确定的唯一的事情是 指定对象在给定时刻的具体类型。

The only thing that is determined at run-time in such cases case is which specific type the pointed object has at the given moment.

这篇关于typeid为多态指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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