0

I need to overload operator - for my complex type.I try to use that, but that's not helps.

myVector & operator -( myVector & iVect )
{
    int size = iVect.getSize();
    myVector temp( size );

    for( int i = 0; i < size; ++i )
        temp[ i ] = m_Vect[ i ] - iVect[ i ];

    return myVector( temp );
}

And i Try to overload copy constructor.

myVector( myVector & iVect ) : m_Vect( iVect.m_Vect ){}

I need this for using objects like c = a - b;

YYY
  • 191
  • 16
  • 1
    What is it that isn't working? – qrikko Dec 10 '13 at 08:14
  • 1
    For one thing, don't return a reference to a temporary, that is Undefined Behaviour. Return a new object. – BoBTFish Dec 10 '13 at 08:17
  • yes, this doesn't works. – YYY Dec 10 '13 at 08:17
  • I change return value to no reference. That works. Thank you. – YYY Dec 10 '13 at 08:21
  • 2
    You should think of const-correctness too: a subtraction should not modify either argument, so the prototype should be `myVector operator- (const myVector & iVect ) const` for a member, and `myVector operator-(const myVector& lhs, const myVector& rhs);` for a non-member. – juanchopanza Dec 10 '13 at 09:37

0 Answers0