关于虚拟方法的问题
如果两个方法都声明为虚拟方法,那么调用的 Method1() 的两个实例不应该都是派生类的 Method1() 吗?
我每次都会看到 BASE 然后 DERIVED 被调用。我正在为一次面试做一些回顾,我想确保我能弄清楚这一点。 xD
class BaseClass
{
public:
virtual void Method1() { cout << "Method 1 BASE" << endl; }
};
class DerClass: public BaseClass
{
public:
virtual void Method1() { cout << "Method 1 DERVIED" << endl; }
};
DerClass myClass;
((BaseClass)myClass).Method1();
myClass.Method1();
方法 1 基础
方法1衍生
IF both methods are declared as virtual, shouldn't both instances of Method1() that are called be the derived class's Method1()?
I am seeing BASE then DERIVED called each time. I am doing some review for an interview and I want to make sure I have this straight. xD
class BaseClass
{
public:
virtual void Method1() { cout << "Method 1 BASE" << endl; }
};
class DerClass: public BaseClass
{
public:
virtual void Method1() { cout << "Method 1 DERVIED" << endl; }
};
DerClass myClass;
((BaseClass)myClass).Method1();
myClass.Method1();
Method 1 BASE
Method 1 DERVIED
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不,“C 风格”转换
((BaseClass)myClass)
通过对 myClass 进行切片来创建临时BaseClass
对象。它的动态类型是BaseClass
,它根本不是DerClass
,因此被调用的Method1
是基类方法。myClass.Method1()
是直接调用。由于myClass
是一个对象,而不是引用,因此没有虚拟调度(不需要)。No, the "C-style" cast
((BaseClass)myClass)
creates a temporaryBaseClass
object by slicing myClass. It's dynamic type isBaseClass
, it isn't aDerClass
at all so theMethod1
being called is the base class method.myClass.Method1()
is a direct call. AsmyClass
is an object, not a reference there is no virtual dispatch (there would be no need).不可以,因为虚函数机制仅在通过指针或引用调用函数时才起作用。否则,将使用对象的静态类型来确定要调用哪个函数。
No, because the virtual function mechanism only works if a function is called via a pointer or a reference. Otherwise the static type of the object is used to determine which function to call.
您在这里看到的称为“切片”。将派生类的对象转换为基类会“切掉”不属于基类的所有内容。
在 C++ 中,虚函数仅适用于指针或引用。为了使您的示例正常工作,您必须执行以下操作:
或者您可以这样做
What you are seeing here is called "slicing". Casting an object of the derived class to the base class "slices off" everything that is not in the base class.
In C++ virtual functions work correctly only for pointers or references. For your example to work right, you have to do the following:
Or you could do
由于转换
((BaseClass)myClass)
将myClass
对象从DerClass
切片为BaseClass
,因此只有 <调用 code>BaseClass 的Method1()
实现。为了使多态性正常工作,您必须通过指针:
或引用调用方法:
Because the cast
((BaseClass)myClass)
slices themyClass
object fromDerClass
toBaseClass
, so only theBaseClass
's implementation ofMethod1()
is called.For polymorphism to work properly, you must call the methods via pointers:
or references:
((BaseClass)myClass).Method1(); -->对象切片因此总是会调用基类方法。
真正的多态行为是通过基类指针实现的,基类指针可以包含派生类的任何对象。
因此,要实现您想要的目标,您需要传递派生类对象的地址并将其类型化为基类指针。如下:
((BaseClass *) &myClass)->Method1();
((BaseClass)myClass).Method1(); --> object slicing hence always base class method will get called.
Real polymorphic behaviour is achieved through base class pointer which can contain any objects of derived classes.
Hence to achieve what you want you need to do pass address of derived class object and typecase it to base class pointer. as below:
((BaseClass *) &myClass)->Method1();