Re: Operator as non-function
On Feb 15, 6:13 pm, saneman <y...@dd.com> wrote:
But if I try to overload the + operator I get the same error:
template< class A > class BOB;
template< class A >
BOB<A> operator+(typename BOB<A>::difference_type, BOB<A> const& );
template <class A>
class BOB {
private:
typedef BOB<A> iterator;
public:
typedef typename A::difference_type difference_type;
friend iterator operator + <> (difference_type i, const
iterator& it);
// Added overload of + operator
iterator operator + (const difference_type i) const {
return iterator(1);
}
};
When Compiling I get:
error: declaration of 'operator+' as non-function
error: expected ';' before '<' token
Where the error refers to:
friend iterator operator + <> (difference_type i, const
iterator& it);
Just a guess, but the operator+ member is hiding the global
operator+ template declaration, so once again, the compiler
doesn't know that it is dealing with a template.
Does that mean that overloading of template operators are impossible?
Not at all, but name lookup for operators is a bit special. I
find it good practice to define most operators as just calling
named functions, putting all of the binary operators in global
scope. (The one exception would be the assignment operators.)
=46rom what you've posted, I gather that BOB<A> is in fact an
iterator (into itself?). A random access iterator, since it
supports addition of a difference_type. What's wrong with
defining a member operator+=, and then, after the class template
definition:
template< typename A >
BOB<A>
operator+( BOB<A> const& lhs,
typename BOB<A>::difference_type rhs )
{
BOB<A> result( lhs ) ;
result += rhs ;
return result ;
}
template< typename A >
BOB<A>
operator+( typename BOB<A>::difference_type lhs,
BOB<A> const& rhs,
{
BOB<A> result( rhs ) ;
result += lhs ;
return result ;
}
Or having defined operator+=, add the following two definitions
directly in the class:
friend iterator
operator+( iterator const& lhs, difference_type rhs )
{
BOB<A> result( lhs ) ;
result += rhs ;
return result ;
}
friend iterator
operator+( difference_type rhs, iterator const& lhs )
{
BOB<A> result( rhs ) ;
result += lhs ;
return result ;
}
(In the above, the keyword friend is used to allow defining the
functions in the class without making them members, and not to
allow access to private members. The Barton and Nackman trick.)
FWIW: I usually provide such operators by inheriting from a
class template which defines them, as above. So all I'd have to
write is:
template< typename A >
class BOB
: public Gabi::MixedArithmeticOperators< BOB< A >,
typename A::difference_type >
{
// ...
// but with a public +=( difference_type )
} ;
The case comes up so often, it's worth having a generic
solution.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34