调用父函数中覆盖的子函数 [英] call overwritten child function within parent function

查看:200
本文介绍了调用父函数中覆盖的子函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在c ++中可以从父函数调用子函数。

is it possible in c++ to call a child function from a parent function.

让我们举个例子:父类在函数中定义工作流。然后,工作流调用表示流的一部分的不同方法(parseElementA)。这些函数可以被子类覆盖,如果不是使用标准函数,它是父的一部分。

Let's take an example: The parent class defines in a function (parse) the general workflow. The workflow then calls different methods which represent part of the flow (parseElementA). These functions can be overwritten by the child class, if not the standart function, which is part of the parent shall be used.

我的问题是:我创建一个子对象并执行工作流函数(parse)。当在工作流函数中调用重写的函数(parseElementA)时,它从父级而不是子级调用该函数。
我可以这样做,它调用子中的覆盖函数。

My issue is: I create a child object and execute the workflow function (parse). When the overwritten function (parseElementA) is called within the workflow function it calls the function from the parent and not from the child. What could i do so it calls the overwritten function in child.

    class Parent {
      public:
        void parse() { parseElementA(); }
        virtual void parseElementA() { printf("parent\n"); }
    };

    class Child : public Parent {
      public: 
        void parseElementA() { printf("child\n"); }
    };

    Child child;
    child.parse();

输出为父级。我能做什么,它返回的孩子。

the output is parent. What can I do that it returns child.

非常感谢您的任何建议。

Thank you very much for any advice.

推荐答案

您的代码中的编译器错误,工作正常

After fixing compiler errors from your code, it works fine.

这篇关于调用父函数中覆盖的子函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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