A non-static member function is a function that is declared in a member specification of a class without a static or friend specifier. ( see static member functions and friend declaration for the effect of those keywords)
You either have to use a pointer to member, or a free function.
A non-member function does not have access to the private data of that class. This means that an operator overloading function must be made a friend function if it requires access to the private members of the class.
The function call operator or operator template is declared static if the lambda-expression has no lambda-capture, otherwise it is non-static. If it is a non-static member function, it is declared const ([class. mfct.
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=....
Put that operator inside your class definition. It must be a member because operator=
is special and you would not gain something by writing it as a non-member anyway. A non-member operator has two important main benefits:
For operator=
, both is not usable. Assigning to a temporary result of a conversion does not make sense, and operator=
will need access to internals in most cases. In addition, a special operator=
is automatically provided by C++ if you don't provide one (the so-called copy-assignment operator). Making it possible to overload operator=
as a non-member would have introduced additional complexity for apparently no practical gain, and so that isn't allowed.
So change your code so that it looks like this (this assumes the operator=
is not a copy-assignment operator, but assigning from a list<T>
to something else. This isn't clear from your question):
class MyClass {
...
template<class T>
MyClass& operator=(const list<T>& lst)
{
clear();
copy(lst);
return *this;
}
...
};
It's pretty standard that a operator=
returns a reference to itself again. I recommend you to adhere to that practice. It will look familiar to programmers and could cause surprises if it would return void
all of a sudden.
If you overload an operator as a member function, you should use this template:
class A {
A& operator=(const A& other) {
if (this != &other) {
...
}
return *this;
}
}
Three things to note:
You can also overload an operator external to the class. This isn't relevant to this example because you can't do it with the assignment operator but it's worth noting because in many cases it's superior to member functions. The typical form is:
class A {
friend const A& operator+(const A& a, const A& b);
...
}
const A& operator+(const A& a, const A& b) {
A& ret = ...
return ret;
}
This one returns a const reference so you can't do this:
(a + b) = c
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With