Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize operator overloading in C++

Shulou Source: shulou.com Published: 2022-06-01 18:44:58 09月14日 Update

This article mainly introduces how C++ implements operator overloading, which has certain reference value. Interested friends can refer to it. I hope you will gain a lot after reading this article. Let Xiaobian take you to understand it together.

In C++, we often encounter the problem of overloading operators. In fact, operator overloading must treat operators as

A function, to distinguish its parameters return value, must figure out where the memory is allocated how to reclaim

When an anonymous object is generated and when it is returned using this pointer.

Operators can be completed with friend functions and member functions. Generally speaking, member functions are used, but some

Special cases must use friend function, such as Author: gaopeng QQ:22389860 all right reserved

> Mail: gaopp_200217@163.com

> Created Time: Sat 25 Mar 2017 04:40:31 PM CST

************************************************************************/

#include

#include

#include

using namespace std;

class testop

{

private:

char* mystr;

int len;

public:

testop(const char* instr)

{

this->len = strlen(instr)+1;

this->mystr = new char[this->len];

memset(this->mystr,0,this->len);

strcpy(this->mystr,instr);

}

testop()

{

this->len = 0;

this->mystr = NULL;

}

testop(const testop& b)//copy constructor deep copy

{

this->len = b.len;

this->mystr = new char[b.len];

memset(this->mystr,0,this->len);

strcpy(this->mystr,b.mystr);

}

void printmystr()

{

coutmystr),b.mystr);

return temp;

}

//operator overloading = deep copy using member functions

testop& operator=(const testop& b)

{

if(this->mystr != NULL)//Memory must be freed first

{

delete [] this->mystr;

}

this->len = b.len;

this->mystr = new char[this->len];

memset(this->mystr,0,this->len);

strcpy(this->mystr,b.mystr);

return *this;

}

//operator overload prefix (++), use member functions to support chain test programming

testop& operator++()

{

this->len = this->len+this->len;

char* temp = new char[this->len];

memset(temp,0,this->len);

strcat(strcat(temp,this->mystr),this->mystr);

delete [] this->mystr;

this->mystr = new char[this->len];

strcpy(this->mystr,temp);

delete [] temp;

return *this;

}

//operator overloading followed by (++), using member functions Chain test programming is not supported because an anonymous object is returned

testop operator++(int)

{

testop tempop = *this;

this->len = this->len+this->len;

char* temp = new char[this->len];

memset(temp,0,this->len);

strcat(strcat(temp,this->mystr),this->mystr);

delete [] this->mystr;

this->mystr = new char[this->len];

strcpy(this->mystr,temp);

delete [] temp;

return tempop;

}

//operator overloading

Tags: Functions operators members operators operations memory articles types support programming clocking copy operands time formal parameters prototypes objects analysis generation obvious Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Microsoft Shulou Tech Info NVidia Huawei