From this week onwards, into the lower part of C + + learning, this week to learn something relatively trivial, but it is worth noting the place.
First, the conversion function
A conversion function is a special type of class member function. It defines a user-defined transformation to convert a class object to some other type.
In the declaration of a class, the conversion function can be declared by specifying the keyword operator, followed by the target type of the conversion, and the general format is:
operator type()
Basic rules for conversion functions:
(1) The conversion function can only be a member function, no return value, null argument.
(2) The conversion to void cannot be defined, and the conversion of an array or function type is not allowed.
(3) A conversion is often defined as a const form, because it does not change the value of a data member.
Specific examples are:
classFraction { Public: Fraction (intNumintden): M_numerator (num), M_denominator (DEN) {}operator Double()Const { return(Double) (M_numerator/m_denominator); }Private: intM_numerator;//molecule intM_denominator;//Denominator}; Fraction F (3,5);DoubleD =4+ F;//call Operater double to convert F to double
double d = 4 + f;
This statement first looks for fraction if there are overloaded operators operator + (double, fraction) function, because there is no, so the conversion function is calledoperator double() const
The main point here is to convert F into a double value.
Non-explicit-one-argument Constructor
Example:
classfraction {fraction (intNumintden=1): M_numerator (num), M_denominator (DEN) {} fractionoperator+ (Constfraction&f) {returnfraction (...); }Private: intM_numerator;//molecule intM_denominator;//Denominator}; Fraction F (3,5); Fraction D2= f +4;//Call Non-explicit ctor to convert 4 to fraction (4, 1), and then call operator +
Although the constructor has two parameters (two parameters), but only one argument, and there is no explicit in front of the constructor, it is called Non-explicit-one-argument constructor.
The main point here is to convert 4 into a fraction object by calling the constructor and by overloading the operator "+".
conversion function vs. Non-explicit-one-argument constructor
classfraction {fraction (intNumintden=1): M_numerator (num), M_denominator (DEN) {}operator Double()Const { return(Double) (M_numerator/m_denominator); } Fractionoperator+ (Constfraction&f) {returnfraction (...); }Private: intM_numerator;//molecule intM_denominator;//Denominator}; Fraction F (3,5); Fraction D2= f +4;//[Error] ambiguous
The code above will result in a two semantic appearance, that is, 4 can be converted to fraction can also convert F to double, and 4 to add a double, and then converted to fraction
To make the compilation pass, you can Fraction d2 = f + 4;
change thedouble d2 = f + 4
Explicit-one-argument Constructor
classFraction {ExplicitFraction (intNumintden=1): M_numerator (num), M_denominator (DEN) {}operator Double()Const { return(Double) (M_numerator/m_denominator); } Fractionoperator+ (Constfraction&f) {returnfraction (...); }Private: intM_numerator;//molecule intM_denominator;//Denominator}; Fraction F (3,5); Fraction D2= f +4;//[Error] convertion from double to fraction requested
Because the explicit keyword is added before the constructor, you cannot convert 4 to a fraction type, or you cannot convert F to a double type, add to 4, and then convert the double to fraction.
Explicit this keyword basically only appears in front of the constructor, meaning that you need to explicitly call this constructor to call, there is no covert, automatic conversion operation.
Second, Point-like class
(1) Smart pointer
With regard to the design of the Point-like class, first of all, there must be a real pointer in it, and in order for the smart pointer to be used like a normal pointer, it has to be overloaded with operators * and, in its design.
Week Four notes C + + Boolan