← Back to team overview

dolfin team mailing list archive

Operator overloading for Vector

 

Hi,

I was wondering why +=, *=, etc. are overloaded in the Vector class, but
not addition of Vectors and multiplication of Vectors by scalars.

Hence I tried the following:

namespace dolfin
{
  Vector operator*( double a, const Vector& v )
  {
    Vector res( v );
    res *= a;
    return res;
  }

  Vector operator*( const Vector& v, double a )
  {
    Vector res( v );
    res *= a;
    return res;
  }

  Vector operator+( const Vector& v1, const Vector& v2 )
  {
    Vector res( v1 );
    res += v2;
    return res;
  }
}

but realized the construction of the return object is not possible since
the copy constructor of Vector is explicit.

Is that a design decision that this is not supposed to be possible? I
realize that creating a temporary Vector can be quite expensive.

Florian



Follow ups