I'm struggling with a problem regarding the linking between two derived classes from a template one. Let's say that we have a template base class Param_B which take as parameter one of it's children (Param_S or Param_M). The template parameter of the class Param_B is used further for definition of other local variables( _parent and a callback function _func). For me is necessary to have the Param_S or Param_M for dynamically call of the function.
The problem is that after I create the pointers to the children's and add timers and functions to them I want to have a _link Variable which will enable to exchange information between both children types. E.g. from the Param_S I want to change the timeout of a Param_M timer by using the _link variable. I
#include <stdio.h>
#include <memory>
#include <vector>
class Param_S;
class Param_M;
template <class T>
class Functionn{
Functionn(){};
~Functionn(){};
private:
std::function<void(std::shared_ptr<T>)> _callback;
};
template <class T>
class Timer
{
public:
Timer(){ timeout = 0; };
~Timer(){};
void setTimeout(const int time){ timeout = time; };
std::weak_ptr<Functionn<T>>& getFunction(){ return _func; };
std::weak_ptr<T> getParent(){ return _parent;};
private:
int timeout;
std::weak_ptr<T> _parent;
std::weak_ptr<Functionn<T>> _func;
};
template <class T>
class Param_B {
public:
Param_B(){};
~Param_B(){};
std::vector<std::shared_ptr<Timer<T>>>& getTimers(){
return _timers;
}
void addTimer(std::shared_ptr<Timer<T>> _t){
_timers.push_back(std::move(_t));
}
std::weak_ptr<Param_B> getLink(){
return _link;
}
void setLink(std::weak_ptr<Param_B> link){
_link = link;
}
private:
std::weak_ptr<Param_B> _link;
std::vector<std::shared_ptr<Timer<T>>> _timers;
};
class Param_S : public Param_B<Param_S>
{
public:
Param_S(){ paramS = -2; };
~Param_S(){};
void setValue(){
paramS = 33;
}
int getValue(){ return paramS;};
private:
float paramS;
};
class Param_M : public Param_B<Param_M>
{
public:
Param_M(){ parammM = -4; };
~Param_M(){};
int getValue(){ return parammM; };
private:
int parammM;
};
class sys{
public:
std::vector<std::shared_ptr<Param_S>> getParams(){
return _params;
}
private:
std::vector<std::shared_ptr<Param_S>> _params;
};
class module{
public:
std::vector<std::shared_ptr<Param_M>> getParams(){
return _params;
}
private:
std::vector<std::shared_ptr<Param_M>> _params;
};
void main(){
std::shared_ptr<sys> _sys = std::make_shared<sys>();
_sys->getParams().push_back(std::make_shared<Param_S>());
_sys->getParams()[0]->addTimer(std::make_shared<Timer<Param_S>>());
std::shared_ptr<module> _mod = std::make_shared<module>();
_mod->getParams().push_back(std::make_shared<Param_M>());
_mod->getParams()[0]->addTimer(std::make_shared<Timer<Param_M>>());
_sys->getParams()[0]->setLink(_mod->getParams()[0]); // this not work , the conversion failed
}
Thank you for your help.