重读《C++沉思录》
文章平均质量分 80
铲灰
平凡的一个人
展开
-
《C++沉思录》第17章模板和泛型算法
#include <iostream>#include <string>using namespace std;/** 关于find1使用的数据结构,我们知道的信息* 1.我们正在查找某个类型为int的值* 2.我们正在一个int对象数组中进行查找* 3.我们已经预先知道了数组中元素的数目* 4.我们知道第一个元素的地址*/const int* find1(const int* array, int n, int x){ const int...原创 2021-01-24 10:47:47 · 115 阅读 · 0 评论 -
《C++沉思录第16章》作为接口的模板
#include <iostream>using namespace std;/** 这个sum函数知道3件事情:* 1.它在把一组数加在一起;* 2.它所加的数是整数,并且* 3.它所加的整数以一种特殊的方式存储了起来*/int sum(int* p, int n){ int result = 0; for(int i = 0; i < n; i++) { result += p[i]; } retur...原创 2021-01-17 10:59:34 · 158 阅读 · 0 评论 -
《C++沉思录》第13章访问容器中的元素
/*1.用类来表示概念,将复杂的功能拆分成小模块,每个类只做一件事情。2.Pointer来专门用来访问数据,Array_data类是中间层,避免了用户删除对象指针导致内存泄漏的问题,Array类是专门提供给用户操作的类(层次分明)*////Array用户避免使用指针操作template<class T>class Array{ friend class Pointer<T>;public: Array(unsigned size): ...原创 2021-01-02 15:35:53 · 151 阅读 · 1 评论 -
《C++沉思录》第12章简单数组Array类
#ifndef _ARRAY_H__#define _ARRAY_H__///设计Array类void f(){ int* p; { Array<int> x(20); p = &x[10]; }}/** 存在2个缺陷:* 第一个:Array<T>::operator[]返回一个T&,所以根本无法阻止用户取得它返回的对象的地址,在上面的例子中,Array对象x超出了作用域,而p还指向它的...原创 2020-12-20 10:35:39 · 141 阅读 · 1 评论 -
《C++沉思录》第九章-句柄:一个堂课练习的分析(上)
///想要学会游泳,就得亲自下水,在C++编程中学习C++,这是唯一可行之道#ifndef _PICTURE_H__#define _PICTURE_H__#include <iostream>#include <string.h>using namespace std;class Picture{ friend Picture frame(const Picture& p); friend Picture operator&(con...原创 2020-12-06 10:49:36 · 104 阅读 · 0 评论 -
《C++沉思录》第七章-句柄:第二部分
学习了《C++沉思录》第七章,手动敲下代码,加深点印象抽象出类UseCount,可以使Handle绑定继承自Point*类型的对象上#ifndef _POINT_H__#define _POINT_H__class Point{public: Point():xval(0), yval(0){} Point(int x, int y):xval(x), yval(y){} int x() { return xval; } ...原创 2020-11-22 10:16:01 · 117 阅读 · 0 评论 -
《C++沉思录》第六章-句柄:第一部分
///学习了一下《C++沉思录》第六章,手动敲了下代码,加深印象///本章采用引起计数器的技术避免了对象的复制时进行不必要的开销,同时介绍了指针语义和值语义的区别#ifndef __POINT_H__#define __POINT_H__class Point{public: Point():xval(0), yval(0){} Point(int x, int y):xval(x), yval(y){} int x() const { re...原创 2020-11-08 11:21:11 · 120 阅读 · 0 评论 -
C++沉思录-代理类
#ifndef __VEHICLE_H__#define __VEHICLE_H__class Vehicle{public: virtual double weight() const = 0; virtual void start() = 0; ///虚复制函数 virtual Vehicle* copy() const = 0; virtual const char* name() = 0;};#endif#ifndef __TR...原创 2020-11-01 10:38:56 · 174 阅读 · 0 评论 -
类设计者的核查表
#include <iostream>#include <stdlib.h>using namespace std;///在一个继承体系中,析构函数必须是虚析构函数///在delete 子类指针对象时不仅需要释放父类的资源,还需要释放子类的资源class B{public: B() { cout << "BBBBBBB" << endl; } virtual ~B() { ...原创 2020-10-18 13:55:24 · 86 阅读 · 0 评论