First, your understanding of operator overloading and the number of arguments isn't quite right. When you overload an operator, you need to use the same number of arguments as in the built in operator. Here are a couple of examples:
operator+ is a binary operator, you have to supply two arguments.
operator- is a binary operator, you have to supply two arguments.
operator- is also a unary operator (int a = 1; -a;), you have to supply one argument.
Second, most operators can be overloaded in two ways: as a class method, or a stand-alone function:
class matrix
{
public:
// yadda yadda yadda
matrix operator+(const matrix& n) const;
};
matrix operator+(const matrix& m, const matrix& n);
In the former case, the first operand is implied to be *this, so you only have one formal argument. In the latter case, the operator is not part of a class, so you must explicitly declare both operands as arguments.
Finally, the statement you want to type, C = A + B;, is actually two operators: = and +. operator=() can be overloaded just like other operators; it's unary so:
class matrix
{
public:
// yadda yadda yadda
matrix& operator=(const matrix& source)
{
// implement copying from source to *this
}
}
|