c++ 重载为返回值对象以及引用的情况
来源:互联网 发布:mac safari 历史记录 编辑:程序博客网 时间:2024/06/05 10:58
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include<cstring>
using namespace std;
class mystring
{
public:
char *p;
int n;
mystring() :p(nullptr), n(0)
{
}
mystring(char *p)
{
this->n = strlen(p) + 1;
this->p = new char[this->n];
strcpy(this->p, p);
}
mystring(const mystring&str)
{
cout << "深拷贝" << endl;
this->n = strlen(str.p) + 1;
this->p = new char[this->n];
strcpy(this->p, str.p);
}
mystring operator+(const mystring&str)
{
cout << "赋值+重载" << endl;
mystring tmp;
tmp.n = this->n+strlen(str.p) + 1;
tmp.p = new char[tmp.n];
strcpy(tmp.p, this->p);
strcat(tmp.p, str.p);
return tmp;
}
mystring &operator=(const mystring&str)
{
cout << "赋值=重载" << endl;
char *old = str.p;
this->n = strlen(str.p) + 1;
this->p = new char[this->n];
strcpy(this->p, str.p);
return *this;
}
~mystring()
{
delete[]p;
}
};
int main()
{
mystring str1("hello");
mystring str2("world");
mystring str3 = str1 + str2;
mystring str4(str1 + str2);
cout << str3.p << endl;
cout << str4.p << endl;
mystring str5;
str5 = str1 + str2;
cout << str5.p << endl;
cin.get();
return 0;
}
#include<iostream>
#include<cstring>
using namespace std;
class mystring
{
public:
char *p;
int n;
mystring() :p(nullptr), n(0)
{
}
mystring(char *p)
{
this->n = strlen(p) + 1;
this->p = new char[this->n];
strcpy(this->p, p);
}
mystring(const mystring&str)
{
cout << "深拷贝" << endl;
this->n = strlen(str.p) + 1;
this->p = new char[this->n];
strcpy(this->p, str.p);
}
mystring operator+(const mystring&str)
{
cout << "赋值+重载" << endl;
mystring tmp;
tmp.n = this->n+strlen(str.p) + 1;
tmp.p = new char[tmp.n];
strcpy(tmp.p, this->p);
strcat(tmp.p, str.p);
return tmp;
}
mystring &operator=(const mystring&str)
{
cout << "赋值=重载" << endl;
char *old = str.p;
this->n = strlen(str.p) + 1;
this->p = new char[this->n];
strcpy(this->p, str.p);
return *this;
}
~mystring()
{
delete[]p;
}
};
int main()
{
mystring str1("hello");
mystring str2("world");
mystring str3 = str1 + str2;
mystring str4(str1 + str2);
cout << str3.p << endl;
cout << str4.p << endl;
mystring str5;
str5 = str1 + str2;
cout << str5.p << endl;
cin.get();
return 0;
}
阅读全文
0 0
- c++ 重载为返回值对象以及引用的情况
- C++之返回值为reference引用的情况---补充(6)《Effective C++》
- 函数返回值为引用、非引用及引用左值的情况
- Effective C++(10) 重载赋值操作符时,返回该对象的引用(retrun *this)
- C++运算符重载,+-*/的重载,返回值为什么是对象而不是对象的引用?而流操作符<< 和 >> 却返回的是引用?
- C++运算符重载,+-*/的重载,返回值为什么是对象而不是对象的引用?而流操作符<< 和 >> 却返回的是引用?
- 引用作为函数返回值的情况
- C++,返回对象的引用,语法练习
- C#linq和lamda两种写法返回的集合中的对象元素引用情况不同,循环计算值要小心
- C/C++的区别(默认值、内联函数、函数重载、const、引用、参数、返回值)
- C++函数返回值的特殊情况(返回引用、非引用、引用左值)
- 重载=时返回对象引用与非引用的区别
- 赋值运算符重载函数 返回引用和返回对象的区别
- 函数返回的临时对象为const类型,const引用
- 【C/C++学院】0820-Nullptr/const对象/类指针引用以及mallocfree与newde/类重载运算符/QT加法重载/类的重载赋值运算/自增在前在后差别/赋值重载深浅拷贝/重载下标
- VC编写返回值是字符串,以及有引用型变量为参数的DLL
- 返回值为引用的一点测试
- 函数返回值为引用的情况大多是在赋值函数中,目的是为了实现链式表达
- hdu 4614 Vases and Flowers(线段树+二分)
- 【网络】TCP服务器的实现
- ejs使用注意事项----js放ejs前面最好
- 3D\X20视差效果
- 2017西安交大ACM小学期 刷墙[折半枚举+异或]
- c++ 重载为返回值对象以及引用的情况
- 你不知道的javascript (中)
- 1
- NetBeans无法使用编码GBK安全地打开该文件
- java—变量
- hadoop UDF-获取前N周的日期时间点
- 随记
- 必读 | 六月份不容错过的十大重磅好文,机器学习和数据科学的小伙伴拿走不谢 原创 2017-07-10 爱收藏的 AI科技大本营 作者 | Flavian Hautbois 翻译 | AI科技大
- Chapter 2.1 Hadoop的安装