[petsc-users] Assignement operator overloading for vectors

Barry Smith bsmith at mcs.anl.gov
Thu Jun 19 08:06:57 CDT 2014


  “an error” is not very informative. Always send all possible information about errors you get; cut and paste all error messages etc.Also better to send the entire (hopefully small) code that reproduces the problem, than abstract snippets.

   Barry

On Jun 19, 2014, at 7:14 AM, Cedric Doucet <cedric.doucet at inria.fr> wrote:

> 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