在C ++中调用将类方法重写为链 [英] Calling overriden class methods as a chain in C++

查看:105
本文介绍了在C ++中调用将类方法重写为链的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不止一次,我觉得需要定义类方法,以类似于构造函数或析构函数的方式调用。

on more than one occasion I felt the need to define class methods that get called in a manner similar to constructors or destructors.

一个具体的例子是:在一个程序中,我有一个非常复杂的不同类型的节点网络,它们以非常不规则的方式相互依赖(网络根本不像树)。当一个节点需要被销毁时,它在网络中开始了一个复杂的破坏链。很像一个蜘蛛网被撕裂,但更复杂。

A specific example would be; in a program I had a very complex network of nodes of different types that depended mutually on each other in a very irregular fashion (The network did not resemble a tree at all). When a node needed to be destroyed, it started a complex chain of destructions in the network. Much like a spider web being torn apart, but more complex.

在这个链的执行过程中,控制回到启动器的方法链中的中间元素),所以当链条已经定居时,必须发生实际的破坏,这就是为什么我不能使用析构函数来实现这个目的。然而,沿着我的节点的类层次,我需要一个析构函数,即调用我的非破坏预破坏函数的梯形图方法(为什么一个实际的析构函数也被称为这种方式,类层次结构中的每一步都需要以不同的方式为链条做出贡献)。

During the execution of this chain, the control came back to the methods of the initiator (or one of the intermediate elements in the chain), so that the actual destruction had to take place when the chain had settled, and that's why I couldn't use destructors for this purpose. However, along the class hierarchy of my nodes, I needed a "destructor like", i.e. a ladder way of calling my non-destructing pre-destruct function (for exactly the same reasons why an actual destructor is also called that way, namely, every step in the class hierarchy needed to contribute to the chain in a different way).

我最终手动编写梯形图。也就是说,类nodeBase有一个称为preDestroyNodeBase的方法,它执行它的工作,并调用虚拟方法preDestroyNode等等,直到叶(我知道,这样看起来像一个构造函数,但它是 - 比较 - 更优雅这样,因为你可以只调用preDestroy的最基础的类)。

I ended up coding the ladder by hand. Namely, the class nodeBase has a method called "preDestroyNodeBase" which does its job and calls the virtual method "preDestroyNode" and so on until the leaf (I know, this way it looks like a constructor, but it was -comparatively- more elegant that way, since you can just call the "preDestroy" of the most base class).

你可以想象这种方法是多么容易出错,更不用说丑陋。有没有更干净的模拟构造函数或析构函数调用方法的方法?某种模板魔法甚至宏魔法!因为手编码太容易出错,即使对于一个程序员来说也是如此,所以我不能想象会把这种行为暴露给图书馆的客户。

You can imagine how error prone this approach is, not to mention ugly. Is there a cleaner way of emulating constructor or destructor way of calling methods? Some kind of template magic or even macro magic! Because hand coding it is too error-prone, even for a single programmer, so I cannot imagine exposing this kind of behavior to the clients of a library.

m缺少一个基本的编程概念,取消了对这些功能的需要。如果是这样,我很高兴如果你指出如何处理节点示例网络。

Maybe I'm missing a fundamental programming concept that obsoletes the need for such functions. If that is the case, I'd be glad if you pointed how else that network of nodes example could be handled.

非常感谢!

推荐答案

当你声明一个虚拟函数时,它将调用函数的最大派生处理程序,然后从那里你有每个处理程序调用 NextMostDerivedClass :: preDestroy ,并且调用将转到函数的次最多的处理程序,您可以再次调用 NextMostDerivedClass :: preDestroy ,等等。这与虚拟析构函数采用的路径相同,除了你不必用析构函数手动调用任何东西,它是自动的。如果你把 cout 语句从下面的代码示例放入析构函数,你可以看到与下面的示例相同的输出。

When you declare a virtual function it will call to the most-derived handler of the function, and then from there you have each handler call NextMostDerivedClass::preDestroy and the calls will go to the next-most-derived handler of the function, and you can again call NextMostDerivedClass::preDestroy, and so on. This is the same as the path a virtual destructor takes, except that you don't have to call anything manually with destructors, it's automated. If you were to put the cout statements from the below code sample into your destructors, you could see the same output as the sample below provides.

#include <iostream.h>

class Foo
{
    public:
    virtual void PreDestroy()
    {
        cout << "Foo preDestroy";
    }
}

class Bar : public Foo
{
    public:
    void PreDestroy()
    {
        cout << "Bar preDestroy\n\n";

        Foo::PreDestroy();
    }
}

class MostDerived : public Bar
{
    public:
    void PreDestroy()
    {
        cout << "MostDerived preDestroy\n\n";

        Bar::PreDestroy();
    }
}

int main() 
{
    MostDerived testObj;

    testObj.PreDestroy();
}

输出应为:

MostDerived preDestroy

MostDerived preDestroy

Bar preDestroy

Bar preDestroy

Foo preDestroy

Foo preDestroy

这篇关于在C ++中调用将类方法重写为链的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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