C ++ operator overload conversion Operator

Source: Internet
Author: User

 Why do we need conversion operators?
We all know that we can use forced conversion characters to convert data of built-in types, for example, (int) 2.1f; custom classes are also types, therefore, the custom class objects also need to support this operation in many cases. C ++ provides the conversion operator overload function, which makes the forced conversion of custom class objects possible.

The conversion operator has a special life type. The method is as follows:

Operator class name ();

The overload function of the conversion operator isNo return typeIt is the same as the class constructor and destructor, and does not follow the rules of the function's return type,None of them return values..

Here is an example to see how it works:

C ++ code

// Example 1

// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

# Include <iostream>
Using namespace std;

Class Test
{
Public:
Test (int a = 0)
{
Cout <this <":" <"load constructor! "<A <endl;
Test: a =;
}
Test (Test & temp)
{
Cout <"load copy constructor! "<Endl;
Test: a = temp.;
}
~ Test ()
{
Cout <this <":" <"load destructor! "<This-> a <endl;
Cin. get ();
}
Operator int () // conversion operator
{
Cout <this <":" <"loads conversion operator functions! "<This-> a <endl;
Return Test:;
}
Public:
Int;
};
Int main ()
{
Test B (99 );
Cout <"B's memory address" <& B <endl;
Cout <(int) B <endl; // strong Conversion
System ("pause ");
}


In this example, we use the conversion operator to forcibly convert the objects of the Test class to the int type and output the data. Observe the running status of the conversion operator function and find that no temporary objects are generated, it proves that it is not the same as a common function, although it carries a return statement.

In many cases, the strong conversion operators of classes can also be used as class objects and operation overload functions. Although their meanings are different, the following example uses the conversion operators, convert the two class objects to int, add them, create temporary class objects, and then assign them to another object.

The Code is as follows:

C ++ code

// Example 2

// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

# Include <iostream>
Using namespace std;

Class Test
{
Public:
Test (int a = 0)
{
Cout <this <":" <"load constructor! "<A <endl;
Test: a =;
}
Test (Test & temp)
{
Cout <"load copy constructor! "<Endl;
Test: a = temp.;
}
~ Test ()
{
Cout <this <":" <"load destructor! "<This-> a <endl;
Cin. get ();
}
Operator int ()

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.