What Does "Operator = Must Be a Non-Static Member" Mean

What does operator = must be a non-static member mean?

Exactly what it says: operator overloads must be member functions. (declared inside the class)

template<class T>
void list<T>::operator=(const list<T>& rhs)
{
...
}

Also, it's probably a good idea to return the LHS from = so you can chain it (like a = b = c) - so make it
list<T>& list<T>::operator=....

Error while overloading operator (must be a nonstatic member function)

You are missing class name:

This is global operator, = cannot be global:

S &operator=(const S &s)

You must define this as class function:

S & S::operator=(const S &s)
// ^^^

Why operator () [] - = must be non-static member?

Because you have to call it on an instance of a class. Take for example the -> operator. How would you propose getting a pointer to the class itself? It doesn't make much sense.

C++ error C2801: 'operator =' must be a non-static member

error C2801: 'operator =' must be a non-static member

The bolded word is key here. friend is not a member; it's a friend. Remove that friend keyword and treat operator= as member:

The syntactically proper version is:

template <class T>
class Matrice
{
T m,n;
public:
template <class V>
Matrice<V>& operator=(const Matrice<V> &);
};

template <class T>
template <class V>
Matrice<V>& Matrice<T>::operator=(const Matrice<V> &M)
{
/*...*/
return *this;
}

Although I think that it's wrong to use that template <class V> there; the sematically proper version would be

template <class T>
class Matrice
{
T m,n;
public:
Matrice<T>& operator=(const Matrice<T> &);
};

template <class T>
Matrice<T>& Matrice<T>::operator=(const Matrice<T> &M)
{
/*...*/
return *this;
}

Explanation: you don't generally want to assign Type<V> to Type<T> in this way; if you have to, then it is probably sign of bad design.

Which operators can only be declared as non-static member functions?

According to the C++ standard §13.5/6:

An operator function shall either be a non-static member function or be a non-member function and have at least one parameter whose type is a class, a reference to a class, an enumeration, or a reference to an enumeration. It is not possible to change the precedence, grouping, or number of operands of operators. The meaning of the operators =, (unary) &, and , (comma), predefined for each type, can be changed for specific class and enumeration types by defining operator functions that implement these operators. Operator functions are inherited in the same manner as other base class functions.

Particularly, the following operators can only be declared as non-static member functions:

  1. Assignment = (§13.5.3)

    An assignment operator shall be implemented by a non-static member function with exactly one parameter.
    Because a copy assignment operator operator= is implicitly declared for a class if not declared by the
    user (12.8), a base class assignment operator is always hidden by the copy assignment operator of the
    derived class.

  2. Function call () (§13.5.4)

    operator() shall be a non-static member function with an arbitrary number of parameters.

  3. Subscripting [] (§13.5.5)

    operator[] shall be a non-static member function with exactly one parameter.

  4. Class member access -> (§13.5.6)

    operator-> shall be a non-static member function taking no parameters.



Related Topics



Leave a reply



Submit