1.主要的内容

  • 泛型编程
  • 面对对象的继承关系(底层内容)
  • 书籍: Effective (modern)C++ 标准库——> 如何用,如何实现

转换函数 conversion function

1 20

class Fraction { public: Fraction(int num, int den = 1) : m_numerator(num), m_denominator(den) { }

// 转换函数
operator double() const {
	return (doubl) (m_numerator / m_denominator);
}

private: int m_numerator; int m_denominator; }

// 调用 Fraction f(3, 5); // 先检查是否重载了+,然后尝试将f转换成允许的类型(还是要检查需要的函数是否被重载) double d = 4 + f;

non-explicit-one-argument constructor

1 24

class Fraction { public: Fraction(int num, int den=1) : m_numerator(num), m_denominator(den){ }

Fraction operator+(const Fraction& f){
	return Fraction(...);
}
// 在这里的案例中,会产生歧义,这会报错(这使得两种可以的选择)
//operator double() const {
//	return (doubl) (m_numerator / m_denominator);%%tor / m_denominator);
//}

private: int m_numerator; int m_denominator; }

// 调用 Fraction f(3, 5); // 这里会将3转换成Fraction类型,以适应重载函数 double d = f + 3;

1 25

class Fraction { public: // explicit:不会自动调用 explicit Fraction(int num, int den=1) : m_numerator(num), m_denominator(den){ }

operator double() const {
	return (doubl) (m_numerator / m_denominator);
}

Fraction operator+(const Fraction& f){
	return Fraction(...);
}

private: int m_numerator; int m_denominator; }

// 调用 Fraction f(3, 5); // 这里3将不能转换为Fraction,会报错(double 无法转换成Fraction) double d = f + 3;

4.pointer-like classes

智能指针

1 34

template class shared_ptr { public: T& operator*() const { return *px; }

T* operator->() const
{ return px; }

shared_ptr(T* p) : px(p){ }

private: T* px; long* pn; …. };

// 调用 struct Foo { …. void method(void) {….} };

shared_ptr sp(new Foo); Foo f(*sp);

// 这里比较特殊,箭头操作符在执行之后会继续对返回值执行操作 // 注意这里的→操作符用于获取指针,同时还用于获取指针所指向的类中的方法 sp->method(); // 等价 px->method();

迭代器

5.function-like classes 仿函数

我们将任何一个能够接受”()”(函数调用符号)的东西叫做函数或者时像函数的东西。 实际上,就是尝试对”()“进行重载

1 5

template struct identity : public unary_function { // 在标准库中会继承这样的base classes const T& operator()(const T& x) const { return x; } };

6.namespace 经验谈

这样在进行测试的时候可以使用重名的函数,不用去想其他的名字(Great thoughts)

1 28

using namespace std;

// ——————————- #include #include

namespace jj01 { void test_member_template() { ….. } } // ——————————–

include

include

namespace jj02 { template using Lst = list<T, allocator>;

void test_template_param()
{ ... } }

int main (int argc, char** argv) { jj01::test_member_template(); jj02::test_template_template_param(); }

9. 成员模板

在模板函数中,在被调用之前函数可以被编译,在传入参数之后,这个函数会被重新编译,这可能导致编译失败(不支持的类型)

1 23

template <class T1, class T2> struct pair { typedef T1 first_type; typedef T2 second_type;

T1 first;
T2 second;

pair()
	: first(T1()), second(T2()) { }
pair(const T1& a, const T2& b)
	: first(a), second(b) { }

// 成员模板(外部被确定之后,内部还是不确定的)
template <class U1, class U2>
pair(const pair<U1, U2>& p)  // 这里是要求传入的初始值需要能够转换成T类型(一次作为初始化值,是T的子类)
	: first(p.first), second(p.second) };

pair<Derived1, Derived2> p; pair<Base1, Base2>p2(p); | pair<Base1, Base2>p2(pair<Derived1, Derived2>());

子类指针能够指向父类,但是反之不能

鲤鱼是一种鱼类,但是鱼类不是鲤鱼

1 2

Base1* ptr = new Derived1; // up-cast shared_ptrsptr(new Derived1); // 模拟up-cast

10、11. 模板特化

泛化

1 2

template struct has { };

特化

1 4

template<> struct hash{ size_t operator() (char x) const { return x; } };

偏特化

1 27

// 数量特化:只对其中一个参数进行特化(不完全特化) template<typename T, typename Alloc=…> class vector { ….. };

template <typename Alloc=….> class vector<bool, Alloc>{ …. } // 只有一个参数被指定了

// 范围特化:指定特化的范围 template class C { .... };

// 这里的模板变量与前面的不是同一个 template class C<T*> // 指定的是任意的指针 { ..... };

// 调用 C obj1; C<string*> obj2; // 调用的函数不同

12.模板模板参数

1 21

template<typename T, template class Container > class XCls { private: Container c;

public: ….. };

// —————————- template using Lst = list<T, allocator>;

// —————————- // 这种参数能够让用户传入一个未被指定的容器 XCls<string, list>mylst1; // 直接使用会报错 XCLs<string, Lst> mylst2;

14.三个主题

variadic templates(C++11):数量不确定的模板

1 16

void print() { // 最后会仅仅传入0个参数,这时候会调用这个重载函数 }

// … 实际上是一种操作符了 template <typename T, typename… Types> void print(const T& firstArg, const Types&… args) { // 查看包的大小: sizeof…(args) cout « firstArg « endl; print(args…); // 递归打印其中的组合的元素 }

// 调用 print(7.5, “hello”, bitset<16>(377), 42);

auto(C++11)

1 9

list c; ... list::iterator ite; ite = find(c.begin(), c.end(), target);

// 使用auto,仅用于类型推导 auto ite = find(c.begin(), c.end(), target); //但是不能用于申明变量 auto ite; // ×

ranged-base for

1 11

vector vec; ....

for(auto elem : vec) { // copy ….. }

// 要修改原始数据,则需要传引用(引用就是指针) for(auto& elem : vec){ elem *= 3; }

15. Reference

引用类型一定要设定初值

1 7

int x = 0; int* p = &x; int& r = x; // r代表x,现在r, x 都是0。 实际上就是一个别名 int x2 = 5;

r = x2; // r不能重新代表其他实物,心啊在r, x都是5 int& r2 = r;

对象和他的reference大小相同,地址也相同(全都是假象) reference通常不用与声明,而是用于参数类型和返回类型的描述

1 6

// 下面两种函数不能共存.二者之间的签名一致 double imag (const double& im); double imag (const double im);

// 这时候的函数签名与上面不同 double imag (const double& im) const { … }

注意: const也是签名的一部分,

16. 复合&继承关系下的构造和析构

1 6

// 由内到外调用构造函数 Derived::Derived(..):Base() {….};

// 由外到内调用析构函数 Derived::~Derived(…){….~Base()};

关于vptr和vtbl

vtbl: virtual table, 为了实现C++的多态,C++使用了一种动态绑定的技术。这个技术的核心是虚函数表

一个类占用了多少的内存,主要看类中声明的变量。继承不仅仅会继承变量,同时也会继承父类的函数的调用权,所以父类有虚函数,子类一定有。 当子类重载虚函数的时候,会重新申请一个函数空间,让对应的虚表中的元素指向这个空间。

这里子类重载了父类的虚函数,所以在子类的虚表中,对应的虚表中的指针会指向一个新的虚函数(B::vfunc1()),所以这三次继承下来,一共会产生四个虚函数。 对于p的调用,用C语言写出来是

1 2

((p->vptr[n])(p); /或者图中下方的形式/ /这时候,调用出来的函数是不确定的,需要根据p指向的类型进行确定,这个就是动态绑定(虚机制)*/

静态绑定&动态绑定 动态绑定的条件:

2 4

18.关于this

在C++类中,都有一个隐藏的参数this

19. 动态绑定

关于const:const对象能够调用const函数,不能调用非const函数,非const对象两者都可以调用。实际上一个常量对象会被多个用户使用(指向同一个对象),当某一个用户需要修改的对象的时候,只需要将其做一份拷贝,这个过程称为Copy on Write. 所以,设置两种不同的函数,一种给常量对象调用,一种是给非常量对象调用,同时,在C++中,规定常量成员函数的const和non-const版本同时存在,const对象只能调用const版本,non-const只能调用non-const版本。

22.示例

当一个对象拥有虚函数的时候,则对应的会多出一个指针(虚指针) 构建对象数组的时候,每一个元素都会调用一次构造函数 分配数组内存的时候,会额外申请一块,用于记录数组的大小(多一个counter)

23.重载new() & delete()

前提:1.每一版本的声明都必须是独特的参数列表,参数列表的第一参数必须是size_t,其余参数以new所指定的placement arguments为初值,出现在new(...)小括号内的就是placement arguments。

1 3

void* operator new(size_t size, long extra){ return malloc(size + extra); }