mercredi 29 juillet 2015

Convert parent to child

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 tis 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. Problem is when I call init method. Example: User creates new algatorc project called Sorting. So he ends up with three classes: SortingTestSetIterator, SortingTestCaseand SortingAbsAlgorithm. In this SortingAbsAlgorithm he inverits 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 posible? 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?

Aucun commentaire:

Enregistrer un commentaire