林锐书:写一个hello world by seasoned professional
2013-11-04 18:07
369 查看
#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
相关文章推荐
- 用php编写一个函数实现 以下功能:字符串hello_world转换成为HelloWorld, get_field_by_id转换成为GetFieldById
- 用g++编写一个hello,world
- 用c++写一个 “hello,world” 的 FastCGI程序
- 使用TCP协议编写一个网络程序,设置服务器端的监听端口是8002,当与客户端建立连接后,服务器端向客户端发送数据“Hello, world”,客户端收到数据后打印输出。
- 打造一个Hello World OSGi Web应用程序
- Hello,world!:搭建一个swt/JFace开发环境
- Spring MVC 学习笔记1 - First Helloworld by Eclipse【& - java web 开发Tips集锦】
- Hello World--------------LeapMotion API新建一个程序
- 写一个简单的scala的hello world出现的问题
- Hello World by Android Studio
- Basic Spring Step by Step Hello World Example
- 自己动手搭建 Redis 环境,并建立一个 .NET HelloWorld 程序测试
- Hello World by Microsoft Speech SDK 5.1
- 定义一个字符串“Hello World ! Coffee”,将此字符串拆分成Hello,World,Coffee三个单词
- 用Vim写一个简单的Hello,World程序
- Windows下qt的环境配置及运行一个c++"hello world"实例详细步骤
- 一个简单的"Hello World"
- 一个简单的 go 语言 hello world.
- 1.3创建一个“HelloWorld"程序
- java算法求一个字符串的倒序(单词不变输出world the Hello)