在施工期间调用虚拟功能 [英] Call virtual functions during construction

查看:46
本文介绍了在施工期间调用虚拟功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,每个人,

我知道我不应该在施工或破坏期间调用虚函数。

hello every body,
I know I shouldn''t call virtual functions during construction or destruction.

展开 | 选择 | 换行 | 行号

推荐答案

想想这个。


当你有一个虚函数时,你指示编译器在调用base之间有一个选择函数或派生函数,派生函数被调用。


因此,如果你有:


类基础

{

public:

虚拟v oid方法();

};
Think about this.

When you have a virtual function, you instruct the compiler that when there is a choice between calling the base function or the derived function, the derived function is to be called.

Therefore, if you have:

class Base
{
public:
virtual void Method();
};


想想这个。


当你有虚拟功能时,你指示编译器在调用基函数或派生函数之间有选择时,将调用派生函数。


但是,在构造过程中无法保证派生函数对象已经创建了。因此,当创建对象的基础部分时,调用将转到基本虚拟函数。


在销毁期间反向应用。由于对象以相反的顺序被销毁,因此在destirctor中调用虚函数会导致对本地函数的调用,因为对象的派生部分可能已经被销毁。


net result是在构造函数和析构函数中忽略virtual关键字。
Think about this.

When you have a virtual function, you instruct the compiler that when there is a choice between calling the base function or the derived function, the derived function is to be called.

However, during construction there is no guarantee that the derived object has been created yet. Therefore, when the base portion of the object is created, the call goes to the base virtual function.

The reverse appplies during destruction. Since the object is destroyed in reverse order, calling a virtual function in a destirctor results in a call to the local function since the derived portion of the object may have already been destroyed.

The net result is the the virtual keyword is ignored inside constructors and destructors.


感谢您的帮助。

我知道在构造期间将调用基本虚函数。所以你说的不是我想知道的。

我想找到答案:为什么这部分代码可以顺利进行:
Thanks for your help.
I know during construction the base virtual function will be called.So What you said is not that I want to know.
I want to find the answer :why this part of code can go well:
展开 | 选择 | Wrap | 行号


这篇关于在施工期间调用虚拟功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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