In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
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
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.