Question about predefined values ​​for operators

In the “C ++ Programming Language” on page 265, the author makes the following statement:

Due to a historical accident, the operators = (assignment) and (address-of) and (sequencing; §6.2.2) have predefined values ​​when applied to class objects. These predefined values ​​may not be accessible to regular users, making them private:

The following is an example:

class X {
private:
 void operator=(const X&);
 void operator&();
 void operator,(const X&);
 // ...
};

void f(X a, X b)
{
   a = b;  // error: operator= private
   &a;     // error: operator& private
   a,b;    // error: operator, private
}

I can’t understand what these error comments refer to. Does this mean that I should not define a function like f, or that all operators =, &and ,should be used by default, and there is no need to redefine them?

+5
source share
6

, , ( , ).

, b a ( a = b), , .

, .

/ , , ( ), , , -, . , , .

, : , , . , , - , , , . , , , , , .

+8

, =, & , .
, , , private, .

private, , . , .

+5

, . . , .

, , , . , .

+1

"X" "=", "&" "," . , , ... .

+1

f , . , , . // error , , , .

+1

, , . ", ++ ".

++ , (, -). , , . , , . , , , , , .

In function f, the author will show you that these statements will not compile due to how the statements are defined in the class. This is perfectly acceptable for overriding statements for your class, and sometimes it is definitely required (for example, to implement a deep copy of the pointer variable in your class). The point of the example is to show that a) you can provide your own implementation of these operators for your class, and b) because of this, you control whether the operators are supported and executed correctly for your class.

0
source

All Articles