静态成员函数和派生类的问题 [英] Issue with static member function and derived class

查看:178
本文介绍了静态成员函数和派生类的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类有静态成员函数(这是必要的)。能够使用类的非静态成员。我定义了一个 Static_This 这是一个指向该类的指针。

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;
};

我在构造函数中设置了 Static_This

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;
}

我在派生类中实现虚函数Total_Energy >

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")); 
}

我从派生类虚函数调用基类的函数。我唯一的问题是,只要我从我的派生类调用基类的成员函数,然后数据(这里是解决方案)被损坏。即当我在上面的例子中调用Add_Term函数时,基类的Solution向量突然被破坏。它就像它被解除分配。

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.

推荐答案

这听起来像问题源于派生类,设计它可以在任何地方。你似乎没有使用任何语言结构的预期目的...你违反了所有的规则,它不清楚为什么。

It sounds like the problem originates in the derived class, and with this kind of design it could be anywhere. You don't seem to be using any language constructs for the intended purpose… you're breaking all the rules and it's not clear why.


当我在上面的例子中调用Add_Term函数的基类的Solution向量突然变得损坏。

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.

忽略代码,当您增加 std :: vector ,它可以重新分配(即,取消分配并移动)包含的对象到一个更大的内存块。因此,你不能在大小增加的 std :: vector 中使用指针或迭代器。

Ignoring the code, when you increase the size of a std::vector, it may reallocate (that is, deallocate and move) the contained objects to a larger memory block. Therefore you can't use pointers or iterators into a std::vector whose size increases.

的程序通过调用 Add_Term 来跟踪 ij_Indexes 中的内容,必须

If some part of the program keeps track of a thing inside ij_Indexes across a call to Add_Term, it must


  • 使用整数偏移量( std :: size_t 是最好的类型)和下标运算符code> ij_Indexes [offset] )或

  • ij_Indexes 的类型应更改为容器 std :: deque ,它不会重新分配,但不是一个平面数组,并且与基于C的数学库不兼容,如你似乎正在使用。

  • Use integer offsets (std::size_t is the best type) and the subscript operator (syntax like ij_Indexes[offset]) or
  • The type of ij_Indexes should be changed to the container std::deque, which doesn't reallocate but isn't a flat array and would be incompatible with a C-based math library such as you appear to be using.

这篇关于静态成员函数和派生类的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆