What is the role of operator overloading? It allows you to provide an intuitive interface for the users of your class. Operator overloading allows the C + + operators to have a user-defined meaning on a user-defined type (Class). Overloaded operators are the syntax modifiers of function calls:
class Fred {
public: // …
};
#if 0 // 没有算符重载:
Fred add(Fred, Fred);
Fred mul(Fred, Fred);
Fred f(Fred a, Fred b, Fred c)
{
return add(add(mul(a,b), mul(b,c)), mul(c,a)); // 哈哈,多可笑…
}
#else // 有算符重载:
Fred operator+ (Fred, Fred);
Fred operator* (Fred, Fred);
Fred f(Fred a, Fred b, Fred c)
{
return a*b + b*c + c*a;
}
#endif
What are the benefits of operator overloading?
By overloading the standard operators on the class, you can explore the intuition of the user of the class. The language used by the user program is problem-oriented, not machine-oriented. The ultimate goal is to reduce the learning curve and reduce the error rate.
What is an instance of an operator overload? Here are some examples of operator overloading:
MyString + yourstring can connect two std::string objects
mydate++ can add a Date object
A * b can multiply two number objects
A[i] can access an element of an Array object
x = *p can dereference a "smart pointer" that actually "points" to a disk record-it actually navigates to the record that P points to on the disk and returns it to X.
But the operator overload makes my class ugly; isn't it supposed to make my class clearer? Operator overloading makes it easier for the user of the class to work, not for the developer of the Class! Consider the following example:
class Array {
public:
int& operator[] (unsigned i);
};
inline
int& Array::operator[] (unsigned i)
{ // …
}
Some people don't like operator keywords or some weird syntax in a class. However, the operator overload syntax is not expected to be used to make the work of a class developer more straightforward. It is expected to be used to make the work of the user of the class simpler:
int main()
{
Array a;
a[3] = 4; // 用户代码应该明显而且易懂…
}
Remember: In a world of reuse, there are many people who use your class, and there is only one person to build it, so you should take care of the majority rather than the minority in everything you do.
What counts as RP/cannot be overloaded? Most of them can be overloaded. The operator of C is only. And? : (and sizeof, technically can be regarded as an operator). C + + added some of its own operators, in addition to:: And. *, most can be overloaded. This is an example of a subscript operator (it returns a reference). No operator overload first:
class Array {
public:
int& elem(unsigned i) { if (I > 99) error(); return data[i]; }
private:
int data[100];
};
int main()
{
Array a;
a.elem(10) = 42;
a.elem(12) += a.elem(13);
}
现在用算符重载给出同样的逻辑:
class Array {
public:
int& operator[] (unsigned i) { if (I > 99) error(); return data[i]; }
private:
int data[100];
};
int main()
{
Array a;
a[10] = 42;
a[12] += a[13];
}
Can I overload operator== to compare two char[] for string comparisons? No: Overloaded operators, at least one operand must be a user-defined type (most of the time a class). But don't do this even if C + + is allowed. Because you should use classes like std::string instead of character arrays here, because arrays are harmful. So you wouldn't want to do that anyway.
Can I create a operator** for the power operation? No way. The name, precedence, binding, and number of the operators are fixed by the language. There is no operator** in C + +, so you cannot create it for a class type.
If you have any questions, consider x * * y equals x * (*y) (in other words, the compiler assumes Y is a pointer). In addition, operator overloading is simply a syntax modification of function calls. Although this particular grammatical modification is wonderful, it does not add anything to the essence. I suggest you overload the Pow (base,exponent) (double version in).
Incidentally, operator^ can be a power operation, but priority and binding are wrong.
How do I create a subscript operator for the matrix (matrix) class? [recently changed so it uses New-style headers and the std:: syntax (on 7/00). Click the next FAQ in the "chain" of recent changes.]