林锐书:写一个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 ...
随机推荐
- SQL数据库查询一张表新建一个排序字段并根据某列的排序存储排序值
现在有一张表如下Id Name Age Classify Score1 张一 18 一班 122 张二 17 二班 19 3 张三 19 三班 30 我跟据他们的分数进行排名 再去新建一个列存储排序值 ...
- java语言的各种输入情况-ACM
1.只输入一组数据: Scanner s=new Scanner(System.in);int a=s.nextInt();int b=s.nextInt(); 2.输入有多组数据,没有说明输入几组数 ...
- pageHelper 分页插件使用
第一步:引入pageHelper的jar包. 链接:https://pan.baidu.com/s/1m3EyAmd_eoAsay0aM7uEkA 提取码:xmfi 第二步:需要在SqlMapConf ...
- jstl 遍历数据
1 导入 jstl 的 jar 包 2. 页面中添加 <%@ taglib prefix="c" uri="http://java.sun.com/jsp/ ...
- 安卓app上传到应用宝、360手机助手、小米应用商店、百度手机助手/安卓市场/91助手
1.小米应用商店 小米开放平台网站:https://account.xiaomi.com 注册帐号教程地址:http://dev.xiaomi.com/doc/?p=90 应用提交流程:http:// ...
- Oracle KEEP的用法
[摘录自] http://blog.itpub.net/12932950/viewspace-687036/ http://flyfx.iteye.com/blog/1994993 聚合函数MIN, ...
- 一个矩阵 JavaScript
//矩阵运算的函数 ;(function(global){ global.Matrix = { //生成对角矩阵,非零元素都为1 eye : function( n ){ var result = [ ...
- 论文笔记 | Self-organized Text Detection with Minimal Post-processing via Border Learning
论文链接:http://openaccess.thecvf.com/content_ICCV_2017/papers/Wu_Self-Organized_Text_Detection_ICCV_201 ...
- Android四层架构
Andrid系统的体系结构设计为多层结构,这种结构在给用户提供安全保护的同时还保持了开放平台的灵活性.如下图所示: Google官方提供的Android系统的四层架构图 从上到下进行简单介绍: 一 ...
- 在SourceTree中使用Git submodule
在開發的過程中我們的項目可能會引用其他的版本庫中的代碼, 例如公司已經累積了一套公用的函式庫, 被多個項目調用; 很顯然地, 不能把公用函式庫的文件直接放到我們開發中的項目中, 這樣不但項目的冗餘, ...