Operator Overloading

1.重载一元操作符

To declare a unary operator function as a nonstatic member, you must declare it in the form:

ret-type operatorop()

where ret-type is the return type and op is one of the operators listed in the preceding table.

To declare a unary operator function as a global function, you must declare it in the form:

ret-type operatorop( arg )

where ret-type and op are as described for member operator functions and the arg is an argument of class type on which to operate.

 

There is no restriction on the return types of the unary operators. For example, it makes sense for logical NOT (!) to return an integral value, but this is not enforced.

 

2.重载二元操作符

To declare a binary operator function as a nonstatic member, you must declare it in the form:

ret-type operatorop( arg )

where ret-type is the return type, op is one of the operators listed in the preceding table, and arg is an argument of any type.

To declare a binary operator function as a global function, you must declare it in the form:

ret-type operatorop( arg1, arg2 )

where ret-type and op are as described for member operator functions and arg1 and arg2 are arguments. At least one of the arguments must be of class type.

 

There is no restriction on the return types of the binary operators; however, most user-defined binary operators return either a class type or a reference to a class type.

 

3.operator=

The assignment operator (=) is, strictly speaking, a binary operator. Its declaration is identical to any other binary operator, with the following exceptions:

  • It must be a nonstatic member function. No operator= can be declared as a nonmember function.

  • It is not inherited by derived classes.

  • A default operator= function can be generated by the compiler for class types if none exists.

 

4.operator()

实现Function object

 

5.operator[]

The subscript operator must be a nonstatic member function that takes a single argument. This argument can be of any type and designates the desired array subscript.

 

6.operator->

Class member access can be controlled by overloading the member access operator (–>). This operator is considered a unary operator in this usage, and the overloaded operator function must be a class member function. Therefore, the declaration for such a function is:

class-type *operator–>()

where class-type is the name of the class to which this operator belongs. The member access operator function must be a nonstatic member function.

This operator is used (often in conjunction with the pointer-dereference operator) to implement "smart pointers" that validate pointers prior to dereference or count usage.

The . member access operator cannot be overloaded.

 

7.Increment and Decrement

 

8.Conversion Functions

conversion-function-name:

operator conversion-type-name ()

conversion-type-name:

type-specifier-list ptr-operatoropt

Conversion functions are often called "cast operators" because they (along with constructors) are the functions called when a cast is used.

Conversion functions are inherited in derived classes. Conversion operators hide only base-class conversion operators that convert to exactly the same type. Therefore, a user-defined operator int function does not hide a user-defined operator short function in a base class.

Only one user-defined conversion function is applied when performing implicit conversions. If there is no explicitly defined conversion function, the compiler does not look for intermediate types into which an object can be converted.

If a conversion is required that causes an ambiguity, an error is generated. Ambiguities arise when more than one user-defined conversion is available or when a user-defined conversion and a built-in conversion exist.

// spec1_conversion_functions2.cpp
#include <string.h>
#include <stdio.h>

struct String {
   // Define constructor that converts from type char *.
   String( char *szBuffer ) {
      strcpy_s( _text, szBuffer );
   }

   // Define conversion to type char *.
   operator char *() {
      return _text;
   }

   int operator==( const String &s )  {
      return !strcmp( _text, s._text );
   }

private:
   char _text[80];
};

int main() {
   String s( "abcd\0" );
   char *ch = "efgh";

   // Cause the compiler to select a conversion.
   return s == ch; // C2666
}

In the expression s == ch, the compiler has two choices and no way of determining which is correct. It can convert ch to an object of type String using the constructor and then perform the comparison using the user-defined operator==. Or it can convert s to a pointer of type char * using the conversion function and then perform a comparison of the pointers.

Because neither choice is "more correct" than the other, the compiler cannot determine the meaning of the comparison expression, and it generates an error.

注:若在构造函数前加上explicit就不存在歧义了

posted @ 2012-11-05 11:55  avexer  阅读(315)  评论(0编辑  收藏  举报