在c ++中向上转换和向下转换 [英] Up-Casting and down-casting in c++

查看:62
本文介绍了在c ++中向上转换和向下转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们有一个基类 BASE 和派生类 DERIVED (派生自 BASE class),那么下面的语句将被视为是表示它是向上转换还是向下转换而不是演员表。



< pre lang =c ++> BASE * pb = new DERIVED();





这里我认为如果我们在基类中有一个虚函数,并且在派生类中重写了相同的函数,那么当我们使用pb调用该函数时,它将调用派生类函数。在这种情况下,该语句将视为向下转换。

另一方面,如果我们在 BASE 类中有一个非虚函数,我们用 pb调用该函数然后它会调用基类函数。然后上面的声明视为上传。

所以请清楚我的疑问,还是说它不是一个演员?



谢谢事先。

解决方案

我们正在谈论仅在转换指针或引用时进行转换,在该指针或引用上调用方法与转换无关。此声明是作业和向上转播。当您在基类指针上调用虚方法时,派生类的override方法通过dynamic dispatch执行,当然只有当实际创建的实例具有虚拟函数的更多派生版本时,否则基类虚函数被执行。在非虚拟方法调用的情况下,没有什么可以解释...



在虚函数调用的情况下会发生什么:将指针强制转换为基类指针但是具有虚方法的对象也有一个隐藏的内部vtable(虚方法表),在创建对象时会对其进行初始化。由于您创建了DERIVED类的实例,因此即使您将指针强制转换为BASE指针,vtable也将指向DERIVED类的虚方法表(并且由于对象将从自身知道派生类的vtable,它是一个DERIVED实例,即使你通过基类指针访问它。通过将指针转换为基类指针,您正在访问可以使用DERIVED指针访问的相同虚拟方法(如果我们忽略很少使用且对程序员来说很少未知的协变返回类型?),则upcast只对虚拟/非虚拟方法和成员变量的可见性。使用基类指针,您很容易看不到派生类中定义的成员变量和非虚方法(这很明显)。也可以在派生类中增加Base类成员的可见性(例如,从protected到public,即使在虚方法的情况下),但这在c ++中很少使用。


If we have a base class BASE and derived class DERIVED (which derives from BASE class), then what does following statement would be treated as means whether it is upcasting or down-casting not a cast.

BASE* pb = new DERIVED();



Here what I think is that if we have a virtual function in base class and same function is overridden in derived class then when we call that function using pb it will call derived class function.in this case this statement treats as downcasting.
On the other hand, If we have a nonvirtual function in BASE class and we call that function with pb then it would call base class function. then above statement treats as up-casting.
So please clear my doubt or is it like that it is not a casting?

Thanks in advance.

解决方案

We are talking about casting only when converting the pointer or reference, calling a method on that pointer or reference has nothing to do with a cast. This statement is an assignment and an upcast. When you call a virtual method on the base class pointer then the override method of the derived class is executed through "dynamic dispatch", of course only if the actually created instance has a "more derived version" of the virtual function otherwise the base class virtual function gets executed. In case of nonvirtual method call there is nothing to explain...

The trick what happens in case of virtual function call: You cast the pointer to a base class pointer but objects having virtual methods also have a hidden internal "vtable" (virtual method table) that is initialized when you create the object. Since you created an instance of the DERIVED class, the vtable will point to the virtual method table of the DERIVED class even if you cast the pointer to a BASE pointer (and because of the vtable of the derived class the object will know from itself that its a DERIVED instance even if you access it through a base class pointer). By casting the pointer to a base class pointer you are accessing the very same virtual methods that you could access with a DERIVED pointer (if we overlook covariant return types that are rarely used and mostly unknown for programmers?), the upcast has effect only on the visibility of virtual/nonvirtual methods and member variables. With a base class pointer you simple don't see the member variables and nonvirtual methods defined in the derived class (this is quite obvious). It is also possible to increase the visibility of Base class members in derived classes (for example from protected to public, even in case of virtual methods) but this is rarely used in c++.


这篇关于在c ++中向上转换和向下转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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