C + + cannot overload the dot symbol,::, sizeof, etc. _c language

Source: Internet
Author: User

Most operators can be overloaded by programmers. The exceptions are:

. (dot symbol)::?: sizeof

There is no fundamental reason to prohibit overloading?:. Just because, I did not find any special circumstances need to overload a ternary operator. Note that an overloaded expression of 1? Expression 2: Function of expression 3, cannot guarantee expression 2: Only one of expression 3 will be executed.

Sizeof cannot be overloaded because of built-in operations (built-in operations), such as incrementally manipulating a pointer to an array, and must depend on it. Consider:

  X a[10];
  x* p = &a[3];
  x* q = &a[3];
  p++; P points to a[4]
  //Then the integer value of p must be a sizeof (X) larger than the integer value of q

Therefore, sizeof (X) cannot be assigned a different new meaning by the programmer, lest it violate the basic syntax.

In N::m, both N and m are not value expressions; N and M are the names that the compiler knows:: Perform a (compile-time) scope resolution instead of an expression evaluation. You can imagine that if you allow overloaded x::y, X might be an object rather than a namespace (namespace) or a class, which would cause--contrary to the original performance--produce a new syntax (Allow expression 1:: Expression 2). Obviously, this complexity does not bring any benefits.

Theoretically,. (dot operators) can be overloaded by using the same techniques as->. However, doing so leads to the problem of not being sure that the operation is overloaded. Object or pass. An object that is referenced. For example:

Class Y {public
  :
  void F ();
  // ...
};
Class X {//Suppose you can overload it.
  y* p;
  y& operator. () {return *p;}
  void f ();
  // ...
};
void g (x& X) {
  x.f ();//X::f or Y::f or error?
}

Attach some summary of C + + overload

Restrictions on overloaded operators:

1 Not all operators can be overloaded. Apart from. ,.* ,:: ,? : Sizeof,typeid These operators cannot be overloaded, and other operators can be overloaded
2 overload cannot change the semantics of the operator for built-in types, and the programmer cannot change the meaning of the operator + for the two int type.
The parameters of the 3 operator function have at least one reference to the object of the class or to the object of the class. This rule prevents programmers from using operators to alter the functions of built-in types.
4 overload cannot change the precedence of an operator.
5 overload cannot change the binding law of an operator.
6 overload cannot change the number of operator operands. For example, + requires two operands, then the overloaded + must also have two operands.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.