林锐书:写一个hello world by seasoned professional
#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的更多相关文章
- 硬盘上的一些算法小题目||and今天看了下林锐的书以及gdb调试 及一些变成算法小题目
gdb调试:观察点,断点,事件捕捉点.step 进入函数,next 跳过函数,until 跳出循环,finish 结束函数 林锐:书后试题 & c++的对象模型图 看了二叉树的非递归遍历, 链 ...
- 用vue写一个仿简书的轮播图
原文地址:用vue写一个仿简书的轮播图 先展示最终效果: Vue的理念是以数据驱动视图,所以拒绝通过改变元素的margin-top来实现滚动效果.写好css样式,只需改变每张图片的class即可实现轮 ...
- 高质量C++/C编程指南(林锐)
推荐-高质量C++/C编程指南(林锐) 版本/状态 作者 参与者 起止日期 备注 V 0.9 草稿文件 林锐 2001-7-1至 2001-7-18 林锐起草 V 1.0 正式文件 林锐 20 ...
- 手把手写一个html_json信息源
html_json用于从网页里提取json数据. 这里用新浪读书的书讯举个例子,手把手写一个html_json信息源. 打开新浪读书的首页,可以看到页面下方有最新.书讯.童书.小说等几个Tab,这里我 ...
- python 拼写检查代码(怎样写一个拼写检查器)
原文:http://norvig.com/spell-correct.html 翻译:http://blog.youxu.info/spell-correct.html 怎样写一个拼写检查器 Pete ...
- 写一个程序,统计自己C语言共写了多少行代码,Github基本操作
前言 在上一篇博客中,本人提到了自己的文件操作可以说是几乎没用过.现在想想,这也算是只在OJ上做题的一个弊端吧.虽然通过OJ做题是一个学习代码好手段,但其他方面也要多多涉猎才好,而不是说OJ用不到文件 ...
- 用C写一个web服务器(四) CGI协议
* { margin: 0; padding: 0 } body { font: 13.34px helvetica, arial, freesans, clean, sans-serif; colo ...
- 爬虫入门 手写一个Java爬虫
本文内容 涞源于 罗刚 老师的 书籍 << 自己动手写网络爬虫一书 >> ; 本文将介绍 1: 网络爬虫的是做什么的? 2: 手动写一个简单的网络爬虫; 1: 网络爬虫是做 ...
- 一起学习造轮子(二):从零开始写一个Redux
本文是一起学习造轮子系列的第二篇,本篇我们将从零开始写一个小巧完整的Redux,本系列文章将会选取一些前端比较经典的轮子进行源码分析,并且从零开始逐步实现,本系列将会学习Promises/A+,Red ...
随机推荐
- 组合数取模介绍----Lucas定理介绍
转载https://www.cnblogs.com/fzl194/p/9095177.html 组合数取模方法总结(Lucas定理介绍) 1.当n,m都很小的时候可以利用杨辉三角直接求. C(n,m) ...
- PIE软件介绍
1. 产品概述 PIE(Pixel Information Expert)软件是北京航天宏图信息技术股份有限公司自主研发的新一代遥感影像处理软件,北京航天宏图信息技术股份有限公司是国内知名的卫星应用服 ...
- 【3dsMax安装失败,如何卸载、安装3dMax 2011?】
AUTODESK系列软件着实令人头疼,安装失败之后不能完全卸载!!!(比如maya,cad,3dsmax等).有时手动删除注册表重装之后还是会出现各种问题,每个版本的C++Runtime和.NET f ...
- git换行符问题
from: http://www.cnblogs.com/flying_bat/archive/2013/09/16/3324769.html 一.AutoCRLF#提交时转换为LF,检出时转换为CR ...
- 1、java线程模型
要了解多线程,先需要把java线程模型搞清楚,否则有时候很难理清楚一个问题. 硬件多线程: 物理机硬件的并发问题跟jvm中的情况有不少相似之处,物理机的并发处理方案对于虚拟机也有相当大的参考意义.在买 ...
- BNU34058——干了这桶冰红茶!——————【递推】
干了这桶冰红茶! Time Limit: 1000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java class nam ...
- Java学习第十八天
1:Map(掌握) (1)将键映射到值的对象.一个映射不能包含重复的键:每个键最多只能映射到一个值. (2)Map和Collection的区别? A:Map 存储的是键值对形式的元素,键唯一,值可以重 ...
- bzoj 5298: [Cqoi2018]交错序列
Description 我们称一个仅由0.1构成的序列为"交错序列",当且仅当序列中没有相邻的1(可以有相邻的0).例如,000,001 ,101,都是交错序列,而110则不是.对 ...
- [转]Create Custom Exception Filter in ASP.NET Core
本文转自:http://www.binaryintellect.net/articles/5df6e275-1148-45a1-a8b3-0ba2c7c9cea1.aspx In my previou ...
- [转]微信小程序,开发大起底
本文转自:http://blog.csdn.net/baiyuzhong2012/article/details/54378497 作者简介:张智超,北京微函工坊开发工程师,CSDN微信开发知识库特邀 ...