C + + operator overload (6) Overload conversion operator __c++

Source: Internet
Author: User

The type converter takes a common form:

Operator/* Overload type */() {/

		* content */
	}

Any return type that can be a function (except void) can be overloaded

converting to an array or function type is not allowed, and it is possible to convert to a pointer type and a reference type

operator Int (Small int&); Error:nonmember
class SmallInt
{public
:
int operator int ();//Error:return list
operator int ( int = 0);  Error:parameter list
//...
};

There are three errors in this example

(1) The conversion function must be a member function

(2) Type conversions cannot be physical parameters

(3) Type conversions cannot have return values


Although the conversion function cannot specify the type returned, each conversion function must display a specified value

For example, operator INT () returns the value of type int, operator Sales_item () returns the value of Sales_item type


Use of conversion operators:

Class Interger {
private:
	int data;  

Public:
	interger (int v) {  
		data = v;
	}

	Overloaded assignment operators can be implemented to assign an int type to the Interger type
	interger& operator = (const int& v) {
		data = V;
		return *this;   Returns the Interger object
	}

	//Type conversion operator: When assigning a interger type to an int type, it implicitly converts the Interger type to an int type and then assigns the Interger
	operator Int ( ) {return
		data;
	}

	Fetch data
	int GetData () {return data
		;
	}
};

Overload =: can Implement (Interger) a = (int) b

Overload Converter int: when (int) b = (Interger) A, first converts the right of the assignment to an int from Interger and assigns A to B

overload int implements an assignment between two variables

int main () {

	int a = 0;
	Interger B (a);

	b = A;
	cout << b.getdata () << Endl;


	System ("PAUSE");
	return 0;
}

Let's look at a special example:

#include <iostream>  
#include <cmath>  
   
using namespace std;  
   
Class Complex  
{  
private:  
    double real;  
    Double imag;  
   
Public:  
    //default constructor  
    Complex (Double r = 0.0, double i = 0.0): Real (R), Imag (i)  
    {}  
   
    ///Find the absolute value of a complex number  
    double m AG ()  
    {return  
        getmag ();  
    }  
   
    Conversion operator  
    operator double ()  
    {return  
        getmag ();  
    }  
   
Private:  
    //Get magnitude  
    double getmag ()  
    {return  
        sqrt (real * real + imag * imag);  
    }  
;  
   
int main ()  
{  
    //define a plural object  
    Complex com (3.0, 4.0);  
   
    To print magnitude, you can use the following two methods  
    /methods 1  
    cout << com.mag () << Endl;  
    Method 2  
    cout << com << endl;  
  

Why does the method 2 here also show the same content as Method 1?

COM is a complex type, Cout<<com<<endl is not allowed

But after the overload conversion operator double COM will be converted from the complex type to double and output by output stream


However, Method 2 should be avoided and Method 2 error prone.


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.