"C + + Primer Plus" Reading notes nine-use class

Source: Internet
Author: User
Tags function definition function prototype

The 11th chapter uses the class

1. Format of operator Function: operator op (argument-list). OP is the operator that will be overloaded.

2. two kinds of invocation methods of operator overloading function : ① function notation: c=a.operator+ (B); A, B, C are all class pairs; ② operator notation: c=a+ B; Object A to the left of the operator is the calling object, and object B to the right of the operator is the object that is passed as a parameter.

3. do not return a reference to a local variable or temporary object , the local variable and the temporary object will disappear when the function finishes executing, and the reference will point to the nonexistent data.

4.C + + restrictions on user-defined operator overloading:The ① overloaded operator does not have to be a member function, but must have at least one operand that is a user-defined type. This prevents the user from overloading the operator for the standard type. ② cannot violate the original syntactic rules of the operator when using the operator, and cannot modify the precedence of the operator. ③ cannot define a new operator. ④ cannot overload the following operators: (sizeof), (.), (. *), (::), (?:), typeID, const_cast, dynamic_cast, Reiniterpret_cast, static_cast. ⑤ The following operators can only be overloaded with member functions: (= assignment operator), (() function call operator), ([] subscript operator), (-).

5. Note: The overloaded member function operator, the operand on the left is the calling object.

6, a non-member function , is not called by an object, and all values (including class objects) used by it are explicit parameters. However, if a function has operations on a class object, non-member functions cannot directly access the private members of the class. At this time, a special kind of function-friend function is required.

7. friend function: Place the prototype in the class declaration and precede the prototype declaration with the keyword Friend:① the friend function is not a member function and therefore cannot be invoked using the member operator. ② Although the friend function is not a member function, it has the same access rights as the member function. Note: When writing a friend function definition, do not use the class scope qualifier (classname::), do not use the keyword friend in the definition.

8. If you want to overload an operator for a class and use a non-class item as the first operand, you can reverse the order of the operands by UF-the unary function, because the first operand must be a class object.

9, cout is a Ostream object, it is intelligent, can recognize all C + + basic types. This is because for each base type, the Ostream class declaration contains the corresponding overloaded operator<< () definition.

10. If you want to output the value of a class object with cout , you can overload the operator<< () function by declaring a friend function in the class, as follows:

Ostream & operator<< (Ostream & os,const time & T)

{

os<<t.hours<<t.minutes;

return OS;

}

Then, time trip;cout<<trip; is transformed into operator<< (Cout,trip);

11, for many operators, you can choose to use member functions or non-member functions to implement operator overloading. In general, a non-member function should be a friend function so that it can access private data directly.

12, Remember: the overloaded operator function of a non-member version requires the same number of parameters as the operator uses, whereas the member version requires fewer arguments because one of the operands is a call object that is implicitly passed through the this pointer.

13, for two time class object T2, T3, Beg t1=t2+t3, can be implemented in two formats , as follows. Note, however , that both formats cannot be selected because both formats match the same expression, and defining both will be treated as ambiguity errors, resulting in compilation errors.

t1=t2.operator+ (T3); member functions

t1=operator+ (T2,T3); Non-member function (friend)

14, because the operator is implemented through the function, so long as the operator function of the same characteristics, the number of operators using the same as the corresponding built-in C + + operators, you can overload the same operator multiple times.

15. When assigning the value of a standard type variable to a variable of another standard type, C + + automatically converts the value to the type of the receiving variable if the two types are compatible. The C + + language does not automatically convert incompatible types (for example: int *p=10; illegal, but can enforce type conversions: int *p= (int*) 10;).

16.C + + allows you to specify how conversions are made between classes and basic types . First, any constructor that accepts a unique argument can be used as a conversion function , converting the same value of the type as the parameter to the class. If you assign a value of the same type as the parameter to an object, C + + automatically calls the constructor. For example: There is a class stonewt, then STONEWT mycat;mycat=19.6; The program uses the constructor STONEWT (double) to create a temporary STONEWT object and 19.6 as the initial value. The contents of the temporary object are then copied to the Mycat by a per-member assignment. This process is called implicit conversion. However, if the constructor's declaration is preceded by the keyword explicit, the constructor cannot be used for this implicit conversion and can only be used for explicit conversions: Mycat=stonewt (19.6);

  17 . When to use STONEWT (double) for implicit conversion?

① when the Stonewt object is initialized to a double value.

② when a double value is assigned to the Stonewt object.

③ to pass a double value to the accept STONEWT parameter.

The ④ return value is declared as STONEWT when the function tries to return a double value.

⑤ in either case, use a built-in type that can be converted to a double type (the built-in type is first converted to double, and then the constructor is called).

18, Stonewt mycat=19.6 equivalent to: ①stonewt mycat (19.6); ②stonewt Mycat=stonewt (19.6); the latter two formats can be used for constructors that accept multiple arguments .

19. Constructors are only used for conversions from a certain type to a class type, and to reverse the conversion (to convert a class to a certain type), a special C + + operator function-the conversion function-must be used . A conversion function is a user-defined coercion type conversion.

  20, for the conversion function, note the following: The ① conversion function must be a class method. The ② conversion function cannot specify a return type. The ③ conversion function cannot have parameters. The specific format is:

Operator TypeName (); For example, a function prototype converted to a double type is: operator double (); Note: A conversion function is a class method that means that it needs to be called through a class object to tell the function what value to convert. Therefore, the function does not require arguments.

  21, the conversion function implicit conversion : Double Mycat1=mycat, if only the double conversion function is defined, so there is no ambiguity, but if you define an int and a double two conversion function operator double (); and operator Int (); the statement long Mycat2=mycat; two semantics rejected! Because both int and double values can be assigned to a long value, the compiler considers using any of the conversion functions to be legal. However, if one is removed, the compiler will accept the statement.

22. When a class defines two or more transformations, it is still possible to explicitly force type conversions to indicate which conversion function to use: Long mycat2= (double) mycat, or Long mycat2=int (MYCAT);

23, the choice to achieve the addition : for example, to add a double (or other built-in type) and Stonewt (class object), there are two options:

① define operator + (const stonewt&,const stonewt&) as a friend function, and let the Stonewt (double) constructor convert a parameter of type double to a parameter of type STONEWT.

② overloads the addition operator to a function that explicitly uses a double type argument: Ⅰ, STONEWT operator + (double X);//member function Ⅱ, friend Stonewt operator+ (double X,STONEWT & S );//friend function

  pros and Cons: The first method relies on implicit conversions to make the program shorter because fewer functions are defined. The disadvantage is that each time the conversion is invoked, the conversion constructor is called, increasing the time and memory overhead. The second method (a function that adds an explicit match type) is just the opposite. It makes the program longer, but runs faster.

"C + + Primer Plus" Reading notes nine-use class

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.