1

Possible Duplicate:
What does “operator = must be a non-static member” mean? (C++)

I'm trying to write an operator= method as a non member, with 2 arguments like this:

template<class T>
T operator=(T & t, const myclass<T>& m)
{
    t = m.val;
    return t;
}

But I get the error that operator= must be a nonstatic member. Is there a compiler flag or some way to trick the compiler to let me run this?

Thanks

Community
  • 1
  • 1
jadams
  • 11
  • 1
  • 4
  • Some operators _must_ be members just because the language requires them to. See [this FAQ entry](http://stackoverflow.com/questions/4421706/operator-overloading/4421729#4421729) for more info. – sbi Jun 27 '11 at 13:27

2 Answers2

2

No there is not, this is mandated by the standard, paragraph 13.5.3.1:

An assignment operator shall be implemented by a non-static member function with exactly one parameter.

Björn Pollex
  • 75,346
  • 28
  • 201
  • 283
0

There isn't, assignment operators need to be declared as members (The rationale is, iirc, to keep you from overriding assignment for primitive or library types).

Alexander Gessler
  • 45,603
  • 7
  • 82
  • 122
  • That rationale seems a little weird. operator+= and others can be overloaded as non member functions. It seems like it would have something to do with visibility of variables in the class (since it must be non static). – jadams Jun 27 '11 at 14:05