[petsc-users] Assignement operator overloading for vectors

Cedric Doucet cedric.doucet at inria.fr
Thu Jun 19 07:14:39 CDT 2014


Hello,

I need to overload the assignement operator for a C++ class which has a Petsc vector as a data member:

class Foo
{
public:
  Foo & operator=(Foo const & copy);
private:
  Vec m_vec;
};

The algorithm for overloading the assignement operator should look like this :

Foo & Foo::operator=(Foo const & copy)
{
  if ( this != copy )
  {
    // destroy this->m_vec
    // allocate this->m_vec with the same size as copy.m_vec's size
    // copy the content of copy.m_vec into this->m_vec
  }
  return *this;
}

I thought that VecCopy(copy.m_vec,m_vec) does everything I need but I have an error during the execution.
So I tried to call first VecDuplicate(copy.m_vec,&m_vec) and then VecCopy(copy.m_vec,m_vec) but I still have an error.
How shoud assignement operator overloading be implemented?
Do I have to call VecDestroy before calling VecDuplicate?
Must input vectors have the same size in VecCopy?

Thank you very much for your help!

Best regards,

Cédric Doucet


More information about the petsc-users mailing list