HPCE / hpce-2017-cw5

1 stars 6 forks source link

Call function from one class in another class #33

Closed natoucs closed 6 years ago

natoucs commented 6 years ago

screenshot from 2017-11-20 19-34-18

How can I call the Execute function in heat_wolrd_v2.cpp, from heat_world.cpp and in the same way that ReferenceExecute is being called ?

guigzzz commented 6 years ago

You can't. The only reason you can call ReferenceExecute from your puzzle solutions is that the puzzle solution classes inherit from the puzzle base class and hence have access to the functions present in the base class (including reference execute). You will need extra work to be able to do this as simply as execute and reference execute. What you can try doing is to put your other execute function in a header file and include that header file from the main puzzle solution header.

m8pple commented 6 years ago

As @guigzzz says, you can't because it is a member function.

However, you can create an instance of one of the classes then call the method. For example, if you have another provider called MiningProviderOther, you could do something like:

#include "mining_provider_other.hpp"

class MiningProvider
  : public puzzler::MiningPuzzle
{
public:
  MiningProvider()
  {}

    virtual void Execute(
               puzzler::ILog *log,
               const puzzler::MiningInput *input,
                puzzler::MiningOutput *output
               ) const override
    {
                std::shared_ptr<puzzler::MiningPuzzle> pOther
                               =std::make_shared<MiningProviderOther>();

        pOther->Execute(log, input, output);
    }
};

You could also turn the member functions into straight functions, assuming they don't need to make use of member variables.

natoucs commented 6 years ago

Thank you!