林锐书:写一个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 ...
随机推荐
- spring4.x hibernate4.x 整合 ehcache 注解 annotate
[From] http://my.oschina.net/alexgaoyh/blog/348188
- 螺旋队列(p98)
先判断这个坐标代表的数位于哪一层,然后依据该层最大的数去计算这个坐标所代表的数. #include"iostream" #include"stdio.h" #i ...
- VUE-CLI 设置页面title
router > index.js { path: '/worklist', name: 'worklist', component: worklist, meta: {title:'维修工列表 ...
- Vim as a Python IDE
参考视频:http://v.youku.com/v_show/id_XNDY4NTM4NzY0.html 好的,在我们默认的centos6的操作系统中使用的python2,我们一般会再去安装一个pyt ...
- CentOS 6.7 安装配置 nagios-server
作者博文地址:https://www.cnblogs.com/liu-shuai/ 一.简介 Nagios是一款开源的免费网络监视工具,能有效监控Windows.Linux和Unix的主机状态, ...
- Beam概念学习系列之SDKs
不多说,直接上干货! https://beam.apache.org/get-started/beam-overview/ Beam SDK 提供了一个统一的编程模型,来处理任意规模的数据集,其中包括 ...
- ICONIX方法(用例分析方法实例教程)
- js控制5秒返回指定界面,或上一个界面
js控制5秒返回指定界面,代码如下 <head> <meta http-equiv="Content-Type" content="text/html; ...
- Starting MySQL. ERROR! The server quit without updating PID file如何解决
今天数据库突然挂了.重启提示: Starting MySQL. ERROR! The server quit without updating PID file (/usr/local/mysql/v ...
- TFS2013 设置签出独占锁(转载)
作者:晓菜鸟 出处:http://www.cnblogs.com/52XF/p/4239056.html 在使用TFS进行源代码管理的时候VS默认允许多个签出,但在团队开发中往往需要设置独占锁(排他锁 ...