
Thinking in C++读书笔记
hqok
这个作者很懒,什么都没留下…
展开
-
Thinking in C++读书笔记--2.1声明与定义
//声明常常使用关键字 extern ,如果只是声明而不是定义它,则要求使用extern//对于函数的声明, extern 是可选的,不带函数体的函数名连同参数或返回值自动的作为一个函数声明extern int i;//声明而没有定义extern float f( float );//函数声明float b;//声明和定义float f( float a )//定义{ return a + 1.0原创 2006-11-08 06:08:00 · 717 阅读 · 0 评论 -
Thinking in C++读书笔记--15.3.2栈模板的实现与测试代码
//15.3.2栈模板#ifndef STACKT_H_#define STACKT_H_template class stacktIter;//先声明,下面的类中要用到该模板。template class stackt{ enum { size = 100 }; T stack[size]; int top;public: stackt() : top(0) { stack[top] =原创 2006-11-16 22:17:00 · 984 阅读 · 0 评论 -
Thinking in C++读书笔记--15.3.1非内联函数的实现
template class array{ enum { size = 100 }; T A[size];public: T& operator[] ( int index );};templateT& array::operator [] ( int index ) //在成员函数的定义中,类名称被限制为模板参数类型:array{ return A[index];}//由...原创 2006-11-10 14:10:00 · 868 阅读 · 0 评论 -
Thinking in C++读书笔记--15.4模板的语法
#include "stdafx.h"#include using namespace std;//15.3模板的语法template class array //这里T是替换参数,它表示一个类型的名称在包容器类中,它将出现在那些原本由某一特定类型出现的地方{ enum { size = 100 };//由于类内部的const常量无法用来做数组的大小,所以采用了enum类型,也是常量 T A[si原创 2006-11-10 09:04:00 · 932 阅读 · 0 评论 -
Thinking in C++读书笔记--13.4继承与组合
#include "stdafx.h"#include using namespace std;//13.4组合与继承的联合class A { int i;public: A( int I ) { i = I; } ~A() {} void f() const {}};class B { int i;public: B( int I ) { i = I; } ~B() {} void f()原创 2006-11-09 08:05:00 · 865 阅读 · 0 评论 -
Thinking in C++读书笔记--5.1范围分解
在C++中,有一个很重要的原因对函数进行重载:构造函数.因为构造函数的名字预先由类的名字确定,所以只能拥有一个构造函数的名字.但我们想用几种方法创造一个对象时该怎么办呢?这是就显现出重载的重要性了.我们对不同的参数使用同样的名字,只要函数的参数不同,编译器就会通过分解这些名字,范围和参数来产生内部名以供链接器使用.仅仅靠返回值来重载函数过于微妙了,所以C++中禁止这样做原创 2006-11-08 07:13:00 · 775 阅读 · 0 评论 -
Thinking in C++读书笔记--10.4指向对象成员的指针
如假有一个结构 : struct sample { int a; };如果有这个结构的对象so与指针sp,可以通过下面的方法选择对象成员:so.a; sp->a;如果有一个指针指向了a,那我们该怎样给它赋值呢?应该是这样的: so.*pm = 47; sp->*pm = 47;指向一个对象的指针的语法变成了->*,而对象则为.*那该怎么定义呢?int sample原创 2006-11-09 00:43:00 · 955 阅读 · 0 评论 -
Thinking in C++读书笔记--10.2C++中的引用
引用像是一个自动能被编译器逆向引用的常量型指针.使用引用时的一些规则:1.当引用被创建时,它必须被初始化2.一旦一个引用被初始化指向一个对象,它就不能改变为对另一个对象的引用.3.不可能有NULL引用函数中的引用,一个例子:int* f( int* x ) { (*x)++; return x;}int& g( int& x ) { x++;}原创 2006-11-09 00:19:00 · 1005 阅读 · 0 评论 -
Thinking in C++读书笔记--5.3缺省参数
缺省参数是在函数声明时就给定一个值,如果我们在调用参数时没有指定这一参数的值,编译器就会自动给这个参数赋上这个值.例:example( int size );example( int size, int quantity );可以用一个函数声明来代替:example( int size, int quantity = 0 );如果有两个定义:example( 100 )原创 2006-11-08 07:50:00 · 715 阅读 · 0 评论 -
Thinking in C++读书笔记--3.3友元
//一个嵌套的struct不能自动的获得存取私有成员的权限//必须遵循特定的规则:首先声明一个嵌套的struct,然后声明它是全局范围使用的一个友元#include "stdafx.h"#include #include #define SZ 20struct holder {private: int a[SZ];public: void initialize(); struct pointer原创 2006-11-08 07:00:00 · 818 阅读 · 0 评论 -
Thinking in C++读书笔记--2.2一个小型的C库
//2.2一个小型的C库的头文件typedef struct STATSHtag { int size; int quantity; int next; unsigned char* storage;}Stash;//注意,这些函数声明用的是标准的C风格函数声明 void initialize( Stash* S , int Size );void cleanup( Stash* S );int原创 2006-11-08 06:10:00 · 768 阅读 · 0 评论 -
Thinking in C++读书笔记--15.3.3模板中的常量
//15.3.3模板中的常量#ifndef TCONST_H_#define TCONST_H_#include #include using namespace std;template class mblock //模板的参数可以使用编译器的内置类型,而且可以使用缺省值{private: T array[size];public: T& operator[] (int index) {原创 2006-11-16 23:54:00 · 1070 阅读 · 0 评论