C + + operator overloading

Source: Internet
Author: User

What is overloading: Let the operator have the function that you want.

#ifndef _array_h_#define_array_h_classarray{Private:    intmlength; int*Mspace; Public: Array (intlength); Array (Constarray&obj); intlength (); voidSetData (intIndexintvalue); intGetData (intindex); ~Array (); //overloading the [] operator, taking values from the array class (int type)    int&operator[](inti); //overloaded operator =, copy array classarray&operator= (Array &AA); //overloaded operator = =, compares two array classes for the same    BOOL operator= = (Array &aa);};#endif
Array.h
#include"iostream"#include"Array.h"using namespacestd; Array::array (intlength) {    if(Length <0) {length=0; } mlength=length; Mspace=New int[mlength];} Array::array (Constarray&obj) {Mlength=obj.mlength; //open up mlength-sized memory space MspaceMspace =New int[Mlength];  for(intI=0; i<mlength; i++) {Mspace[i]=Obj.mspace[i]; }}intarray::length () {returnmlength;}voidArray::setdata (intIndexintvalue) {Mspace[index]=value;}intArray::getdata (intindex) {    returnMspace[index];} Array::~Array () {mlength= -1; Delete[] mspace;}int& Array::operator[](inti) {    return  This-mspace[i];} Array& Array::operator= (Array &AA) {    inti =0; if( This->mspace! =NULL) {         Free(Mspace); }     This->mlength =aa.mlength; Mspace=New int[Mlength];  for(i=0; i<mlength; i++)    {        (* This) [I] =Aa[i]; }    return(* This);}BOOLArray::operator= = (Array &AA) {    inti =0; if( This->mlength! =aa.mlength) {return false; }     for(i=0; i< This->mlength; i++)    {        if(Mspace[i]! =Aa.mspace[i]) {            return false; }    }    return true;}
Array.cpp
#include"iostream"#include"Array.h"using namespacestd;intMain () {Array A1 (Ten);  for(intI=0; I<a1.length (); i++)    {        //A1.setdata (i, I); //Overwrite [] function oneA1[i] =i; }         for(intI=0; I<a1.length (); i++)    {        //printf ("Array%d:%d\n", I, A1.getdata (i));printf"Array%d:%d\n", I, a1[i]); } Array A2=A1; Array A3 ( -); A3= A2;//overloaded operator = function two     for(intI=0; I<a2.length (); i++)    {        //printf ("Array%d:%d\n", I, A2.getdata (i));printf"Array%d:%d\n", I, a2[i]); } a1[9] = +; if(A1==A2)//overloaded operator = = function three{cout<<"two arrays are exactly equal! "<<Endl; }    Else{cout<<"two arrays are not equal! "<<Endl; }    return 0;}
ArrayText.cpp

Overloaded operator Note points:

1. There are two methods for overloading operators:1> using member functions (most of them) 2> with global functions (friend functions): When the source code of the object calling this operator cannot be obtained, such as: Overloading << operator

// The friend function implements the left shift operator, prints the object friend Ostream &operator<< (ostream & Out, Complex &cc);

2. Front + + (-) and post + + (--) Attention points

// The class member function implements the predecessor + + and returns a reference to itself Complex &operator+ +()// class member functions implement Post + +, the return value is local object operator+ + ( int )

3. Special Note: when overloading = or the [] operator, the return is a reference, and only the reference can be an lvalue. It is also important to note that the caller's original pointer field is empty, not empty, and needs to be released!

C + + operator overloading

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.