P16-c++ use class-summary

Article Directory

1. Summary

This chapter introduces many important aspects of defining and using classes, some of which may be difficult to understand, but with the continuous increase of practical experience, you will gradually master them.
Generally speaking, the only way to access private class members is to use class methods. C++ uses friend functions to circumvent this restriction. To make a function a friend, you need to declare the function in the class declaration and add the keyword friend before the declaration.

C++ extends the overloading of operators and allows customizing special operator functions. This function describes the relationship between specific operators and classes. Operator functions can be class member functions or friend functions ( Some operator functions can only be class member functions).
To call an operator function, you can call the function directly, or you can use the overloaded operator in the usual syntax.
For the operator op, the format of the operator function is as follows:
operatorop( argument-list)
argument-list represents the operand of the operator. If the operator function is a class member function, the first operand is the calling object,
which is not in argument-list I. For example, this chapter overloaded addition by defining the operator+() member function for the Vector class. If up, right, and result are all Vector objects, you can use any of the following statements to call vector addition:

result = up.operator+(right);
result up + right;

In the second statement, since the types of operands up and right are both Vector, C++ will use the addition definition of Vector.

When the operator function is a member function, the first operand will be the object that calls the function. For example, in the preceding statement, the up object is the object that calls the function.
When defining an operator function, if you want to make the first operand not a class object, you must use a friend function. In this way, the operands can be passed to the function in the required order.

One of the most common operator overloading tasks is to define the << operator so that it can be used with cout to display the content of the object.
To make the ostream object the first operand, you need to define the operator function as a friend; to make the redefined operator can be spliced ​​with itself, you need to declare the return type as ostream&. The following general format can meet this requirement:

ostream operator<<(ostream &os, const c_name & obj)
{
    
    
	os <<..; //display object contents
	return os;
}

However, if the class contains such a method, it returns the value of the data member that needs to be displayed, you can use these methods without directly accessing these members in operator<<().
In this case, the function need not (and should not) be a friend.

C++ allows you to specify the way to convert between classes and basic types. First, any constructor that accepts only one parameter can be used as a conversion function to convert a value of the same type as the parameter to a class. If you assign the same value as the parameter to the object, C++ will automatically call the constructor.
For example, if there is a String class that contains a constructor that takes a char* value as its only parameter, then if the bean is a String object, you can use the following statement:

bean = "pinto"; //converts type char to type String

However, if the keyword explicit is added before the declaration of the constructor, the constructor will only be used for explicit conversion

bean = String("pinto" ); //converts type char *to type string explicitly

To convert a class object to other types, a conversion function must be defined to point out how to perform this conversion.
The conversion function must be a member function. The prototype of the conversion function that converts a class object to typename is as follows:

operator typename();

Note that the conversion function has no return type and no parameters, but it must return the converted value (although the return type is not declared).
For example, here is the function to convert Vector to double type:

Vector::operator double()
{
    
    
	return a_double_value;
}

Experience has shown that it is best not to rely on this implicit conversion function.

Guess you like

Origin blog.csdn.net/sgy1993/article/details/113777364