#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

林锐书:写一个hello world by seasoned professional的更多相关文章

  1. 硬盘上的一些算法小题目||and今天看了下林锐的书以及gdb调试 及一些变成算法小题目

    gdb调试:观察点,断点,事件捕捉点.step 进入函数,next 跳过函数,until 跳出循环,finish 结束函数 林锐:书后试题 & c++的对象模型图 看了二叉树的非递归遍历, 链 ...

  2. 用vue写一个仿简书的轮播图

    原文地址:用vue写一个仿简书的轮播图 先展示最终效果: Vue的理念是以数据驱动视图,所以拒绝通过改变元素的margin-top来实现滚动效果.写好css样式,只需改变每张图片的class即可实现轮 ...

  3. 高质量C++/C编程指南(林锐)

    推荐-高质量C++/C编程指南(林锐) 版本/状态 作者 参与者 起止日期 备注 V 0.9 草稿文件 林锐   2001-7-1至 2001-7-18 林锐起草 V 1.0 正式文件 林锐   20 ...

  4. 手把手写一个html_json信息源

    html_json用于从网页里提取json数据. 这里用新浪读书的书讯举个例子,手把手写一个html_json信息源. 打开新浪读书的首页,可以看到页面下方有最新.书讯.童书.小说等几个Tab,这里我 ...

  5. python 拼写检查代码(怎样写一个拼写检查器)

    原文:http://norvig.com/spell-correct.html 翻译:http://blog.youxu.info/spell-correct.html 怎样写一个拼写检查器 Pete ...

  6. 写一个程序,统计自己C语言共写了多少行代码,Github基本操作

    前言 在上一篇博客中,本人提到了自己的文件操作可以说是几乎没用过.现在想想,这也算是只在OJ上做题的一个弊端吧.虽然通过OJ做题是一个学习代码好手段,但其他方面也要多多涉猎才好,而不是说OJ用不到文件 ...

  7. 用C写一个web服务器(四) CGI协议

    * { margin: 0; padding: 0 } body { font: 13.34px helvetica, arial, freesans, clean, sans-serif; colo ...

  8. 爬虫入门 手写一个Java爬虫

    本文内容 涞源于  罗刚 老师的 书籍 << 自己动手写网络爬虫一书 >> ; 本文将介绍 1: 网络爬虫的是做什么的?  2: 手动写一个简单的网络爬虫; 1: 网络爬虫是做 ...

  9. 一起学习造轮子(二):从零开始写一个Redux

    本文是一起学习造轮子系列的第二篇,本篇我们将从零开始写一个小巧完整的Redux,本系列文章将会选取一些前端比较经典的轮子进行源码分析,并且从零开始逐步实现,本系列将会学习Promises/A+,Red ...

随机推荐

  1. Kafka使用多个分区时 consumer的Assign配置

    天天在给自己挖坑排坑... 因为要开多线程消费,所以分区加到了10,两个broker. Producer没有做特殊处理,所以是随机发到Partitions. 但是Consumer只做Subscribe ...

  2. (转载) win10生成SSH keys

    (转载) win10生成 SSH keys:   SSH key 可以让你在你的电脑和Code服务器之间建立安全的加密连接.  先执行以下语句来判断是否已经存在本地公钥: cat ~/.ssh/id_ ...

  3. Caused by java.lang.IllegalStateException Not allowed to start service Intent { cmp=com.x.x.x/.x.x.xService }: app is in background uid UidRecord(一)

    Caused by java.lang.IllegalStateException Not allowed to start service Intent { cmp=com.x.x.x/.x.x.x ...

  4. layer弹出层显示在top顶层

    父页面 导入 layer.js 或者 layui.all.js,导入后就能正常显示在父窗口页面区域. 1.显示在顶层窗口 top.layer.open({ type: 2, area: ['98%', ...

  5. Angular4+NodeJs+MySQL 入门-01

    有一定的后台开发经验ES6语法.后台没有用框架来,纯自己写.会sql语句 安装NodeJS的安装 从网上下载 https://nodejs.org/en/ 选择自己,我用的是最新版本 Angular ...

  6. pycharm Python解释器的配置--可以指定批处理文件为解释器

    这样就可以很方便的配置一些环境变量了,很方便很有创意的功能,再次Mark一下以防忘记

  7. 创建Django项目时,settings的静态文件的配置

    STATICFILES_DIRS = ( os.path.join(BASE_DIR,'static'), )

  8. 【Java集合】LinkedList详解中篇

    这是关于LinkedList的第二篇文章,我将会源码分析LinkedList的部分重要代码,关键地方我都有注释说明,希望大家能比较明白的看懂! 分析源码按照顺序分析: 变量 构造方法 方法 一.变量 ...

  9. Java ScheduledExecutorService源码分析

    Java 定时任务可以用Timer + TimerTask来做,或者使用ScheduledExecutorService,使用ScheduledExecutorService有两个好处: 1. 如果任 ...

  10. Scarpy框架持久化存储

    一.介绍 持久化存储操作分为两类:磁盘文件和数据库. 而磁盘文件存储方式又分为:基于终端指令和基于管道 二.基于终端指令的持久化存储 Scrapy是通过 scrapy 命令行工具进行控制的. 这里我们 ...