#include <iostream>
#include <string.h>
using namespace std; class String
{
private:
int size;
char *ptr;
public:
String():size(0),ptr(new char('\0'))
{ cout << "default\n"; } String(const String &s):size(s.size)
{
cout << "1 param constructor\n";
ptr = new char[size + 1];
strcpy(ptr,s.ptr);
} //my code
String(char *s)
{
cout << "2 param constructor\n";
ptr = new char[strlen(s) + 1];
strcpy(ptr,s);
} ~String()
{
delete []ptr;
} friend ostream& operator<<(ostream &,const String & );
String& operator=(const String &s);
}; //c++ primer第四版中文,435页
//14.2.1 输出操作符的重载:为了与io标准库一直,操作符应接受ostream&作为第一个参数,对类类型const对象的引用作为第2个参数,并返回对ostream形参的引用。
/*friend*/ ostream& operator<<(ostream & os,const String &s) //这里的friend去掉了!就像类内的static在外面定义时要去掉一样。
//不去掉的话会报错 error: ‘friend’ used outside of class
{
return (os << s.ptr);
} String& String::operator=(const String &s)
{
cout << "operator=\n";
if(this != &s)
{
delete []ptr;
ptr = new char[strlen(s.ptr) + 1];
strcpy(ptr,s.ptr);
}
return *this;
} int main()
{
String s1 = "hello world";
String s2 = s1;
s2 = s1; cout << s1 <<endl;
cout << s2 <<endl; return 0;
} /work/ctest/public_private$ ./1
2 param constructor
1 param constructor
operator=
hello world
hello world

/work/ctest/public_private$ ./1
2 param constructor
1 param constructor
operator=
hello world
hello world

下面的是林锐的书的原来的代码

#include <iostream>
#include <string.h>
using namespace std; class String
{
private:
int size;
char *ptr; public:
String():size(0),ptr(new char('\0'))
{ cout << "default\n"; } String(const String &s):size(s.size)
{
cout << "1 param constructor\n";
ptr = new char[size + 1];
strcpy(ptr,s.ptr);
} ~String()
{
delete []ptr;
} friend ostream& operator<<(ostream &,const String & );
String& operator=(const char *s);
}; ostream& operator << (ostream & os,const String &s)
{
return (os << s.ptr);
} String& String::operator=(const char *s)
{
cout << "operator=\n";
//if(this != &s)//这一行代码要注释掉,不然会 error: comparison between distinct pointer types ‘String*’ and ‘const char**’ lacks a cast
{
delete []ptr;
ptr = new char[strlen(s) + 1];
strcpy(ptr,s);
}
return *this;
} int main()
{
String s1 ;
s1 = "Hello World"; cout << s1 <<endl; return 0;
}

/work/ctest/public_private$ ./1
default
operator=
Hello World

最新文章

  1. npm install socket.io遇到的问题
  2. PacketiX VPN搭建企业VPN
  3. Oracle导入中文乱码解决办法
  4. 在Service Fabric上部署Java应用,体验一把微服务的自动切换
  5. mysql 查看数据库大小
  6. 人工智能2:智能Agent
  7. call by value or reference ?
  8. 老李分享:Uber究竟是用什么开发语言? 1
  9. leaflet 利用ajax 将前端地图上的数据post到后台
  10. PHP 常用知识点
  11. Burnside引理的感性证明
  12. sql表中数据遍历
  13. PHP中字符串与html相互转化函数
  14. Qt开发之信号槽机制
  15. SQL Server 无法连接数据库
  16. 使用命名管道的OVERLAPPED方式实现非阻塞模式编程 .
  17. VS2017创建一个 ASP.NET Core2.0 应用,并搭建 MVC 框架
  18. 2019.01.21 洛谷P3919 【模板】可持久化数组(主席树)
  19. identify.class.php&lt;======&gt;token加密方法
  20. [笔记] 整除分块 &amp; 异或性质

热门文章

  1. Mysql 忘记密码----修改Navicat的连接密码--以及--(加入安装Navicat时没设置密码)有时新建连接设置密码,连接不成功---的问题解决方法
  2. linux下的常见信号总结
  3. ubuntu中安装kernel-devel
  4. 牛客网Java刷题知识点之equals和hashcode()
  5. SpringMvc上传文件遇到重复读取InputStream的问题
  6. poi 多行合并
  7. Linux抓包工具:tcpdump
  8. BNU 28887——A Simple Tree Problem——————【将多子树转化成线段树+区间更新】
  9. [转](.NET Core C#) AES Encryption
  10. js之cookie操作