将父转换为子(Convert parent to child)

编程入门 行业动态 更新时间:2024-10-28 14:36:31
将父转换为子(Convert parent to child)

我有一个名为AbsAlgorithm类,有三个纯虚函数,如下所示:

class AbsAlgorithm { public: //...other methods virtual void run() = 0; virtual bool init(TestCase&) = 0; virtual void done() = 0; };

这个类在我的可执行文件中,名为algatorc 。 最终用户必须创建algatorc项目,并且还必须实现这三种方法。 但问题是:他还必须继承TestCase类。 这是init方法中的参数。 在我的主程序中,我必须编译用户编写的代码,并构建动态库并将其加载到我的程序中。 我做到了 问题是当我调用init方法时。

例:

用户创建名为Sorting新algatorc项目。

所以他最终得到了三个班:

SortingTestSetIterator SortingTestCase SortingAbsAlgorithm

在这个SortingAbsAlgorithm他继承了AbsAlgorithm并实现了纯虚方法。 在SortingTestCase他必须继承TestCase类,在SortingTestSetIterator他必须继承TestSetIterator并实现名为get_current()方法,该方法返回TestCase 。

我的主程序我将SortingTestSetIterator加载到TestSetIterator ,如下所示:

create_it = (TestSetIterator* (*)())dlsym(handle, "create_iterator_object"); TestSetIterator *it = (TestSetIterator*)create_it();

所以现在,我可以调用TestSetIterator::get_current()方法(此方法返回指向TestCase指针,但用户返回SortingTestCase的对象)。 但是当我调用这个方法时,我得到了TestCase 。 这一切都很好,但是我需要将它传递给AbsAlgorithm::init(...) 。 当然,仍然没有问题,但是当用户实现方法init(...) ,他必须将其转换为子类( SortingTestCase )。 这可能吗?

我知道这在Java中是微不足道的,但我不知道如何在C ++中这样做。 或者它是我定义方法TestCase* TestSetIterator::get_current()然后用户以某种方式重新定义这个以便返回类型是SortingTestCase ? 这会解决问题吗?

基本上,问题是这样的:

我有方法SortingTestSetIterator::get_current()返回指向SortingTestCase类的实例的指针。 那么,将父母转换为孩子是否有某种意义?

I have class called AbsAlgorithm with three pure virtual functions like this:

class AbsAlgorithm { public: //...other methods virtual void run() = 0; virtual bool init(TestCase&) = 0; virtual void done() = 0; };

This class is in my executable, called algatorc. End-user must create algatorc project and must also implement these three methods. But the problem is this: he must also inherit TestCase class. which is parameter in init method. In my main program, I must compile code that user wrote, and build dynamic library and load it into my program. I did that. The problem is when I call init method.

Example:

User creates new algatorc project called Sorting.

So he ends up with three classes:

SortingTestSetIterator SortingTestCase SortingAbsAlgorithm

In this SortingAbsAlgorithm he inherits AbsAlgorithm and implements pure virtual methods. In SortingTestCase he must inherit TestCase class and in SortingTestSetIterator he must inherit TestSetIterator and implement method called get_current(), which returns TestCase.

My main program I loaded SortingTestSetIterator into TestSetIterator, like this:

create_it = (TestSetIterator* (*)())dlsym(handle, "create_iterator_object"); TestSetIterator *it = (TestSetIterator*)create_it();

So now, I can call methods like TestSetIterator::get_current() (this method returns pointer to TestCase, but user returns object of SortingTestCase). But when I call this method, as a result, I get TestCase. This is all ok, but then I need to pass this to AbsAlgorithm::init(...). Sure, still no problem, but when user implemented method init(...), he must convert this to child class (SortingTestCase). Is this possible?

I know that this is trivial in Java, but I don't know how to do this in C++. Or is it a way that I define method TestCase* TestSetIterator::get_current() and then user somehow redefine this so that return type is SortingTestCase? Would this solve the problem?

Basically, problem is this:

I have method SortingTestSetIterator::get_current() that returns pointer to instance of SortingTestCase class. So, is it somehow posible to convert parent to child?

最满意答案

如果你想将Parent转换为Child,你需要做的就是写这个:

child = dynamic_cast<Child*>(parent_object)

但是为了这样做,你的源类(Parent类)必须至少有一个虚方法! 它几乎肯定需要一个虚拟析构函数,否则当你试图清理时你会遇到问题......

If you want to cast Parent to Child, all you need to do is to write this:

child = dynamic_cast<Child*>(parent_object)

But in order to do so, your source class (Parent class) must have at least one virtual method! It almost certainly needs a virtual destructor, or you'll have problems when you try to clean things up...

更多推荐

本文发布于:2023-08-05 11:47:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1432723.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   Convert   child   parent

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!