Implicit conversion and cast overload of C + + classes _c language

Source: Internet
Author: User
Tags class definition

Before writing this article, let's review the compiler through the matching process to determine which function to call the matching order:
(1) to find and use the most qualified function name and parameter type (including return value) of the functions, if found then call;

(2) Otherwise, Look for a function template, instantiate it to produce a matching overloaded function, call it if found;

(3) Otherwise, look for an overloaded function that can be matched by a type conversion, and call it if found.

If none of the above steps find a matching function, the call is wrong, and if there are more than one matching translation of the call, then the call match appears to be two semantic and is also wrong.
 
Type conversions are values that map one type of value to another type. Type conversions actually contain two types of automatic implicit and mandatory. The automatic implicit conversion rules for the internal data types provided by the

C language compilation system are as follows:

1. A low type can be converted to a high type when performing an arithmetic operation.

2. In an assignment expression, the value of the right expression is automatically implicitly converted to the type of the left variable and assigned to it.

3. When an argument value is assigned to a parameter when the function is called, the system implicitly converts the argument to the type of the formal parameter and assigns it to the formal parameter.

4. When a function has a return value, the system automatically converts the return expression type to a function type, assigning to the calling function.

In these cases, the system is implicitly converted. A compilation error occurs when two data types are found incompatible in the program and the implicit conversion cannot be completed automatically. For example:
int* p = 100;
In this case, the compiler will make an error, and in order to eliminate errors, you can do the coercion type conversion as follows:
int* p = (int *); The
explicitly converts an integer of 100 to a pointer type. The

constructor has a type conversion feature

in practice, when a constructor for a single parameter is provided in a class definition, the class provides a way to convert a value or variable of another data type to a user-defined data type. Therefore, it can be said that the constructor of a single parameter provides the function of data conversion. The following is an example to further illustrate the type conversion capabilities of a single parameter constructor.

Copy Code code as follows:

#include
ClassA
{
Public
A () {m=0;}
A (Doublei) {m=i;}
Voidprint () {cout<<m<
Private
Doublem;
};

Voidmain ()
{
Aa (5);
a=10; A and 10 are different data types
A.print ();
}

The output of the program is:
10
In this program, the assignment statement a=10, the value 10 and object A are two incompatible data types, but it can be successfully compiled and the output shows the correct results, the main reason is the benefit of a single parameter constructor. The compilation system chooses to convert the integer value 10 to a double by using a standard data type conversion, and then converts the double value to a Class A type by using the single parameter constructor defined in the class, and assigns it to a at the end. These transformations are automatically done implicitly.

For the above procedure, add one point:
Aa = 10;
And
Aa;
A= 10;
The two are different, the former is first made to a, the compiler tries to convert 10 implicitly to type A, which causes a (Doublei) constructor to be invoked directly.
The latter belongs to an assignment statement, and the compiler creates a temporary object and converts 10 implicitly to type A. If we show the call
(A) 10;
This also creates a temporary object that causes the constructor of a to be invoked.

It is also important to note that the compiler only makes an implicit conversion (except for the built-in type of the C-time library, such as Intshort Char), which is illustrated in the following statement:
M_rst->getfields ()->getitem (Ncol)->value= (_bstr_t) svalue;
The above Value is the variant type of COM, and "value=" will cause operator= (_bstr_t) to be invoked. If the above omission (_bstr_t), the compiler will have an error, because there is no operator= (char*) Such overloads, the compiler will not give us more than two implicit conversion.

Operator overloading and construction is also a function call during a function call, and if the matching function is not ambiguous, an implicit conversion can occur. If the Value variant class of the previous sentence has only one operate= (_bstr_t), then write the->value= svalue; The compiler will also attempt to implicitly convert svalue to the _bstr_t type.

There is also a situation
Copy Code code as follows:

ClassA
{
Inta
Public
A () {};
A (int_a) {a = _a;};
Operatorint () {return A;}
}

Like a downward adjustment:
Copy Code code as follows:

Aa (10);
AA2 = (int) (int) A; is equivalent to AA2 = (int) A; Because the first nearest has been turned int, the second//one does not have to turn again

It's interesting that the Class A has both the construction of an int implicit conversion A and an int () conversion function for casting, and (int) (int) A will be done in the nearest principle. If the conversion fails, the compiler will make an error. Like what:
Copy Code code as follows:

ClassB
{
};
AA2 = (B) A;
Or
AA2 = (B) 10;

The compiler reported an error like: "errorC2440: Type Conversion": Cannot convert from "int" to "B"
We know how important it is to construct and transform functions that we write ourselves.

Conversion functions
A conversion function is also called a type cast member function, which is a non-static member function in a class. It is defined in the following format:
Copy Code code as follows:

class< Type descriptor 1>
{
Public
operator< type descriptor 2> ();
...
}

This transformation function defines the mapping relationship between the < type specifier 1> to the < type descriptor 2>. Visible, conversion functions are used to convert one type of data to another. The following example illustrates the function of a transformation function.
Copy Code code as follows:

#include

Classrational
{
Public
Rational (intd, int n)
{
Den= D;
num= N;
}
Operatordouble ();//Type conversion function
Private
Intden, num;
};

Rational::operatordouble ()
{
Returndouble (DEN)/double (num);
}

Voidmain ()
{
RATIONALR (5, 8);
doubled = 4.7;
d+= R; This sentence will invoke an implicit conversion, equivalent to d= (double) r;
cout<<d<<endl;
}

Program Output results:
5.325

It is known by the program that D is a double value, and r is an object of the rational class, and that the addition of two different types of data is facilitated by the conversion function operatordouble (). To enable the addition to proceed, the compiler examines the description of the class rational to see if there is a conversion function that converts the operand of rational type to a double type. Since the rational class describes the conversion function operatordouble (), it can be converted at the time the program is run, so the d=r operation is implemented in the program.

You should be aware of the following points when defining a transformation function:
(1) The conversion function is a user-defined member function, but it is non-static.
(2) The conversion function can not have a return value. (meaning that there is no return value in the Declaration)
(3) The conversion function also takes no arguments.
(4) Conversion function function can not be defined as a friend function.

The name of the transformation function is the target type of the type conversion, so you do not have to specify the return value type for it; the conversion function is a value or variable that is used for this type to be converted to another type, and does not have to be with parameters.

class to complete other types of conversions to the class type, while overloading casts the complete class type to other types of conversions.

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.