是否有任何自动化的方法来实现构造后和构造前虚拟方法调用? [英] Is there any automated way to implement post-constructor and pre-destructor virtual method calls?

查看:91
本文介绍了是否有任何自动化的方法来实现构造后和构造前虚拟方法调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于从构造函数和析构函数内部调用虚拟方法的众所周知的问题,我通常最终得到需要在它们的构造函数之后调用final-setup方法并调用pre-teardown方法的类.就在他们的析构函数之前,像这样:

Due to the well-known issues with calling virtual methods from inside constructors and destructors, I commonly end up with classes that need a final-setup method to be called just after their constructor, and a pre-teardown method to be called just before their destructor, like this:

MyObject * obj = new MyObject;
obj->Initialize();   // virtual method call, required after ctor for (obj) to run properly
[...]
obj->AboutToDelete();  // virtual method call, required before dtor for (obj) to clean up properly
delete obj;

这是可行的,但是它带有风险,即调用者会忘记在适当的时间调用这两个方法中的一个或两个.

This works, but it carries with it the risk that the caller will forget to call either or both of those methods at the appropriate times.

所以问题是:C ++中有什么方法可以使这些方法自动被调用,因此调用者不必记住要调用它们? (我猜没有,但是我想还是要问一下,以防万一有一些聪明的方法可以做到)

So the question is: Is there any way in C++ to get those methods to be called automatically, so the caller doesn't have to remember to do call them? (I'm guessing there isn't, but I thought I'd ask anyway just in case there is some clever way to do it)

推荐答案

在C ++中添加后构造函数的主要问题是,尚无人知道如何处理后构造函数,后构造函数等等

The main problem with adding post-constructors to C++ is that nobody has yet established how to deal with post-post-constructors, post-post-post-constructors, etc.

潜在的理论是对象具有不变性.这个不变量是由构造函数建立的.一旦建立,就可以调用该类的方法.随着引入需要后构造函数的设计,您将介绍一旦构造函数运行后就不会建立类不变式的情况.因此,允许从后构造函数调用虚拟函数同样不安全,并且您将立即失去它们似乎具有的一项明显好处.

The underlying theory is that objects have invariants. This invariant is established by the constructor. Once it has been established, methods of that class can be called. With the introduction of designs that would require post-constructors, you are introducing situations in which class invariants do not become established once the constructor has run. Therefore, it would be equally unsafe to allow calls to virtual functions from post-constructors, and you immediately lose the one apparent benefit they seemed to have.

如您的示例所示(可能没有意识到),不需要它们:

As your example shows (probably without you realizing), they're not needed:

MyObject * obj = new MyObject;
obj->Initialize();   // virtual method call, required after ctor for (obj) to run properly

obj->AboutToDelete();  // virtual method call, required before dtor for (obj) to clean up properly
delete obj;

让我们展示为什么不需要这些方法.这两个调用可以从MyObject或其基数之一调用虚函数.但是,MyObject::MyObject()也可以安全地调用这些函数. MyObject::MyObject()返回后不会发生任何事情,这会使obj->Initialize()安全.因此obj->Initialize()错误或可以将其调用移至MyObject::MyObject().相同的逻辑适用于obj->AboutToDelete().派生程度最高的析构函数将首先运行,并且仍然可以调用所有虚拟函数,包括AboutToDelete().

Let's show why these methods are not needed. These two calls can invoke virtual functions from MyObject or one of its bases. However, MyObject::MyObject() can safely call those functions too. There is nothing that happens after MyObject::MyObject() returns which would make obj->Initialize() safe. So either obj->Initialize() is wrong or its call can be moved to MyObject::MyObject(). The same logic applies in reverse to obj->AboutToDelete(). The most derived destructor will run first and it can still call all virtual functions, including AboutToDelete().

这篇关于是否有任何自动化的方法来实现构造后和构造前虚拟方法调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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