I have a class with static member function (which is necessary). To be able to use non-static members of the class. I defined a Static_This
which is a pointer to the class.
template<class T>
class Energy_Minimizer
{
protected:
static Energy_Minimizer* Static_This;
Petsc_Vector<T>* Gradient;
Petsc_Vector<T>* Solution;
static int Form_Function_and_Gradient(Petsc_Vector<T> *Solution_,Petsc_Vector<T> *Gradient_, PetscReal *Function_Value_);
public:
Energy_Minimizer(MPI_Comm Communicator_);
void Add_Term(vector<int>& Indexes, void* Coefs, string Function_Name_);
virtual void Total_Energy()=0;
};
I set the Static_This
in the constructor of the class.
template<>
Energy_Minimizer<double>::Energy_Minimizer(MPI_Comm Communicator_)
{
Communicator = Communicator_;
Static_This = this;
...
}
And can access to non-static virtual function:
int Energy_Minimizer<double>::Form_Function_and_Gradient(Petsc_Vector<double> *Solution_,Petsc_Vector<double> *Gradient_, PetscReal *Function_Value_)
{
Static_This->Solution = Solution_;
Static_This->Gradient = Gradient_;
// Call the user-defined routine to construct the function value, gradient
Static_This->Total_Energy();
return 0;
}
I implement the virtual function Total_Energy() in the derived class:
class Strain_Solver : public Energy_Minimizer<double>;
void Strain_Solver::Total_Energy()
{
****** Here problem occurs ******
this->Add_Term(ij_Indexes, NULL , string("Alpha_Term"));
}
I call the function of the base class from derived class virtual function. The only problem I have is that as soon as I call a member function of the base class from my derived class then the data (here Solution) gets corrupted. i.e. When I call Add_Term function in above example the Solution vector of the base class suddenly gets corrupted. It is like it gets de-allocated.